Dictionary in Python
Mappings are a collection of objects that are stored by a key, unlike a sequence that stored objects by their relative position. A Python dictionary consists of a key and then an associated value. That value can be almost any Python object.
Dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key.
- How to construct Dictionary ?
# Make a dictionary with {} and : to signify a key and a value
>>> student_info = {'jack': 111, 'sape': 112}
#jack is key and 111 is value associated with jack.#you can call value by its key
>>> student_info['jack']
'111'#you can add element using assignment
>>> student_info['john']=[1,2,3]
>>> student_info
{'jack': 111, 'sape': 112, 'john':[1,2,3]}
Dictionary is very flexible that it can hold any Python data type in it.
>>> student_info = {'jack': 111, 'sape':[12,23,33]}
#sape is holding list as a value>>> student_info['sape']
[12,23,33]>>> student_info['sape'][0] #you can call an index on that value
12
You can change the values of dictionary.
#subtract 110 from the value
>>> student_info['sape']= student_info['sape'] - 110
>>> student_info['sape']
1#You can use self subtraction/addition also.>>> student_info['sape'] += 110 #adding 110 to value
>>> student_info['sape']
111
Performing list(d.keys())
on a dictionary returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just use sorted(d.keys())
instead).
Some methods which we can call on a dictionary.
>>> student_info.keys() #arbitrary list of keys
['jack','sape','john']>>> sorted(student_info.keys()) # sorted list of keys
['jack','john','sape']>>> student_info.values()
[111,112,[1,2,3]]>>> student_info.items()
[('jack',111),('sape',112),('john',[1,2,3])]
- Nested Dictionary
# Dictionary nested inside a dictionary nested inside a dictionary
>>> dict = {'key1':{'key2':{'key3':'value'}}}#call the value
>>> dict['key1']['key2']['key3']
'value'
- Looping into Dictionary:
When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the items()
method.
>>> student_info = {'jack': 111, 'sape': 112,'jay':90}>>> for key,value in student_info:
... print(key,value)
...
('jack', 111)
('sape', 112)
('jay', 90)
- How to print the values of the dictionary in order?
- An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted. The only difference between dict() and OrderedDict() is that:
- OrderedDict preserves the order in which the keys are inserted. A regular dict doesn’t track the insertion order, and iterating it gives the values in an arbitrary order. By contrast, the order the items are inserted is remembered by OrderedDict.
- For more info, visit following link:
Hope you have git the basic understanding about dictionaries. Lot more to come in this. We will revisit dictionary in next coming chapters!
Happy Coding!