You can learn about Dictionaries in Python Programs with Outputs helped you to understand the language better.

Python Programming – Dictionaries

Dictionaries (or diet in Python) are a way of storing elements just like you would in a Python list. But, rather than accessing elements using its index, you assign a fixed key to it and access the element using the key. Dictionaries are like address books. It is used to hold data that are related. For example, if you know a person’s name, you can easily get a person’s details. The person’s name is technically referred to as a key, and any corresponding detail is referred to as the value. Note that the key must be unique as you cannot find the correct information if you have two persons with the same name.

The dictionary is Python’s built-in mapping type. This means that dictionaries map keys to values and these key-value pairs are a useful way to store data in Python. Keys will be a single element. Val¬ues can be a list or list within a list, numbers, etc. To declare a dictionary, you use curly braces on either side { }. Inside these curly brackets, keys and values are declared. Each key is separated from its value by a colon (:) while each element is separated by commas. In other words, a dictionary is:

  1. An associative array.
  2. A mapping from keys to values.
  3. A container (collection) that holds the key: value pairs.

Concept of key : Value pair

Python dictionary (its keyword is diet) is a data type that stores multiple data items (elements) of different types.

Let’s Try

Write the output of the following:

>>> myDict = {“A”: “Ant”, “C”:”Cow”}
>>> myDict

The dictionary is like a list, but more general. In the list, the indices have to be integers; in the dictionary, they can be (almost) of any type. You can think of a dictionary as a mapping between a set of indices (which are called keys) and a set of values. Each key maps to a value. The association of a key and a value is called a key: value pair or sometimes an item. Pairs of keys and values are specified in the dictionary by using the notation:
d = { key1 : value1, key2 : value2 }
Notice that the key:value pairs are separated by a colon, and the pairs themselves are separated by commas, and all this is enclosed in a pair of curly braces. For example, here is an empty dictionary and several dictionaries containing key:value pairs:
d1 = { }
d2 = {‘width’: 8.5, ‘height’: 11}
d3 = {1: ‘RED’, 2: ‘GREEN’, 3: ‘BLUE’, }
Note that a comma after the last pair is optional. Python does not store dictionary elements in any particular order. If you enter elements in one order, they may be stored in another (essentially random) order inside Python. The order of the key:value pairs is not the same. In fact, if you type the same example on your computer, you might get a different result. In general, the order of items in the dictionary is unpredictable. But that’s not a problem, as the elements of the dictionary are never indexed with integer indices. Instead, you use the keys to look up the corresponding values. For example,
>>> info = {‘Name’: ‘Mona’, ‘Age’: 7, ‘Class’: ‘First’};
>>> print(info)
{‘Name’: ‘Mona’, ‘Class’: ‘First’,’Age’: 7 } ;

  • Note that the order of elements while the dictionary was being created is different from the order in which they are actually stored and displayed.

Python Programming - Dictionaries chapter 9 img 1

Characteristics of a Dictionary

  1. The dictionary is an unordered collection of objects.
  2. Values are accessed using a key.
  3. The dictionary can shrink or grow as required.
  4. The contents of dictionaries can be modified.
  5. Dictionaries can be nested.
  6. Sequence operations, such as slice, cannot be used with dictionaries.
  7. More than one entry per key is not allowed ( no duplicate key is allowed).
  8. The values in the dictionary can be of any type while the keys must be immutable like numbers, tuples, or strings.
  9. Dictionary keys are case sensitive – The same key names with the different cases are treated as different keys in Python dictionaries.
  • A dictionary is used as a content type that can store any number of Python objects, including other con¬tainer types.

Checking for the Existence of a key

Membership operators in and not in work with dictionaries, but they can check for existence of keys only. The in operator returns True if the given key is present in the dictionary, otherwise False. The not in operator will return True if the given key is not present in the dictionary otherwise False.
>>> d = { }
>>> ‘name’ in d
False
>>> dl’name’] = ‘Mohan’
>>> ‘name’ in d
True
>>> stu1={‘name’ : ‘Raj’, ‘age’:17, ‘Marks’:97}
>>> ‘Raj’ in stul # ‘Raj’ is not a key, hence it returned False
False
>>> ‘Raj’ not in stu1
True

The operators in and not in do not apply to values of a dictionary.

Leave a Reply

Your email address will not be published. Required fields are marked *