fbpx Placeholder canvas

Dictionary

The data structures you have learned so far, i.e., strings, lists and tuples use integers as indices and you retrieve the value at a particular index position by [<index_position>]. You will notice an error If you use any other data type. Also frequently you require to store the data based on a key: value which can be a string such as the name of a person. In such cases, dictionary data structure comes handy. Dictionaries allow you to use any other immutable type as an index, such as string, tuple. Each entry in the dictionary contains an index and a value separated by a colon (:). We can add a new object to the dictionary using the [ ] operator.

Dictionaries are used to store data values in key: value pairs. A dictionary is a collection that is ordered*, changeable and does not allow duplicates. Dictionary items are ordered, changeable, and do not allow duplicates.

Example:

thisdict = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
print(thisdict)

Output:

{‘brand’: ‘Ford’, ‘model’: ‘Mustang’, ‘year’: 1964}