Class XI : Python Dictionary

What is a dictionary in Python?

* The dictionary is the data type in Python where some specific value exists for some particular key.

* A Dictionary in Python is the unordered and changeable collection of data values that holds key-value pairs.

* While other compound data types have only value as an element, a dictionary has a key: value pair.

* A Dictionary in python is declared by enclosing a comma-separated list of key-value pairs using curly braces({}).

* Python Dictionary is classified into two elements: Keys and Values.

* Dictionaries are optimized to retrieve values when the key is known.

Properties of Dictionary Keys

There are two important points while using dictionary keys

* More than one entry per key is not allowed ( no duplicate key is allowed)

* The values in the dictionary can be of any type, while the keys must be immutable like numbers, tuples, or strings.

* Dictionary keys are case sensitive- Same key name but with the different cases are treated as different keys in Python dictionaries.

Dictionaries and lists share the following characteristics:

* Both are mutable.

* Both are dynamic. They can grow and shrink as needed.

* Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A dictionary can also contain a list, and vice versa.

Dictionaries differ from lists primarily in how elements are accessed:

* List elements are accessed by their position in the list, via indexing.

* Dictionary elements are accessed via keys.