Python dictionary (Dictionary)
Dictionaries are another mutable container model and can store objects of any type.
Each key-value key=>value pair of the dictionary is separated by a colon : , and each key-value pair is separated by a comma , , and the entire dictionary is enclosed in curly braces {}. The format is as follows:
d = {key1 : value1, key2 : value2 }
The keys are generally unique. If the last key-value pair is repeated, the previous one will be replaced, and the value does not need to be unique.
>>>dict = {'a': 1, 'b': 2, 'b': '3'}; >>> dict['b'] '3' >>> dict {'a': 1, 'b': '3'}
Values can take any data type, but keys must be immutable like strings, numbers or tuples.
A simple dictionary instance:
dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
Dictionaries can also be created like this:
dict1 = { 'abc': 456 }; dict2 = { 'abc': 123, 98.6: 37 };
access value in dictionary
Put the corresponding keys in familiar square brackets, as in the following example:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}; print "dict['Name']: ", dict['Name']; print "dict['Age']: ", dict['Age'];
result:
dict['Name']: Zara dict['Age']: 7
If you access the data with a key that is not in the dictionary, you will get the following error:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}; print "dict['Alice']: ", dict['Alice'];
result:
dict['Alice']: Traceback (most recent call last): File "test.py", line 5, in <module> print "dict['Alice']: ", dict['Alice']; KeyError: ' Alice '
Modify the dictionary
The way to add new content to the dictionary is to add new key/value pairs, modify or delete existing key/value pairs as follows:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}; dict['Age'] = 8; # update existing entry dict['School'] = "DPS School"; # Add new entry print "dict['Age']: ", dict['Age']; print "dict['School']: ", dict['School'];
result:
dict['Age']: 8 dict['School']: DPS School
remove dictionary element
You can delete a single element and also clear the dictionary, and clearing is only one operation.
Display delete a dictionary with the del command, the following example:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}; del dict[ ' Name ' ]; #delete the entry whose key is 'Name' dict.clear(); #clear all entries in the dictionary del dict ; #delete the dictionary print "dict['Age']: ", dict['Age']; print "dict['School']: ", dict['School'];
But this throws an exception because the dictionary no longer exists after del:
dict['Age']: Traceback (most recent call last): File "test.py", line 8, in <module> print "dict['Age']: ", dict['Age']; TypeError: 'type' object is unsubscriptable
Characteristics of dictionary keys
Dictionary values can take any python object without restriction, either standard objects or user-defined, but not keys.
Two important points to remember:
1) Do not allow the same key to appear twice. If the same key is assigned twice during creation , the latter value will be remembered, as in the following example:
dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'}; print "dict['Name']: ", dict['Name'];
result:
dict['Name']: Manni
2) The key must be immutable, so it can be used as a number, string or tuple, so a list cannot be used, as in the following example:
dict = {['Name']: 'Zara', 'Age': 7}; print "dict['Name']: ", dict['Name'];
result:
Traceback (most recent call last): File "test.py", line 3, in <module> dict = {['Name']: 'Zara', 'Age': 7}; TypeError: list objects are unhashable