Storing multiple values of same or different types under a single name. They are collection of data.
Dictionaries are unordered collection of data.
They are key-value mapping.
Represented by Curly braces {}
dictionary : {1: 10, 2: 20, 3: 30, 4: 'Hello', 5: -2.0}
Defined by - key : value.
Each key-value map is separated by comma (,)
Keys | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
Values | 10 | 20 | 30 | 40 | 50 |
dict1 = {1:10, 2:20, 3:30}
Syntax:
dict(iterable = None)
iterable: any data structures listed above given that elements are present as key value mapping.
IN [1]
d1 = {}
type(d1)
IN [4]
l = [(1, 10), (2, 20), (3, 35)]
d2 = dict(l)
print(d2)
type(d2)
stdout
{1: 10, 2: 20, 3: 35}
There are various methods or functions that are used to work with dictionaries.
1. len
2. str
3. clear
4. copy
5. fromkeys
6. get
8. items
9. keys
10. values
11. update
12. pop
13. popitem
10. setdefault
Returns the length of the given iterable.
Syntax:
len(iterable)
IN [5]
d1 = {1: 12, 2: 23, 3: 34}
len(d1)
Returns the string format of the given dictionary.
Syntax:
str(dict)
IN [6]
d1 = {1: 12, 2: 23, 3: 34}
str(d1)
Deletes the items in the dictionary.
Syntax:
dict.clear()
IN [7]
d1 = {1: 12, 2: 23, 3: 34}
d1.clear()
d1
Returns the shallow copy of the given dictionary.
Syntax:
dict.copy()
IN [8]
d1 = {1: 12, 2: 23, 3: 34}
d2 = d1.copy()
d2
Returns a new dictionary form the given list of keys. By default the values are set to None.
Syntax:
dict.fromkeys(sep, val = None)
IN [12]
key = ['key1', 'key2', 'key3']
d2 = dict.fromkeys(key)
d2
IN [16]
key = ['key1', 'key2', 'key3']
d2 = dict.fromkeys(key, 0)
d2
Returns the value of the given key if present else returns the default value given (None by Default).
Syntax:
dict.get(key, default = None)
IN [17]
d1 = {1: 12, 2: 23, 3: 34}
d1.get(1)
IN [19]
d1 = {1: 12, 2: 23, 3: 34}
print(d1.get(4))
stdout
None
IN [20]
d1 = {1: 12, 2: 23, 3: 34}
d1.get(4, 0)
Returns the list of key-value pairs in the dictionary.
Syntax:
dict.items()
IN [25]
d1 = {1: 12, 2: 23, 3: 34}
d1.items()
Returns the list of keys present in the dictionary.
Syntax:
dict.keys()
IN [24]
d1 = {1: 12, 2: 23, 3: 34}
d1.keys()
Returns the list of values present on the dictionary.
Syntax:
dict.values()
IN [26]
d1 = {1: 12, 2: 23, 3: 34}
d1.values()
Adds the key-value pairs in second dictionary to the first.
Syntax:
dict.update(dict)
IN [27]
d1 = {1: 12, 2: 23, 3: 34}
d2 = {4: 45, 5: 56}
d1.update(d2)
d1
Removes and returns the value of given key. If the key is absent, Error is raised if default is not given.
Syntax:
dict.pop(key, [default])
IN [28]
d1 = {1: 12, 2: 23, 3: 34}
print(d1.pop(2))
d1
stdout
23
IN [31]
d1 = {1: 12, 2: 23, 3: 34}
print(d1.pop(4))
d1
Error
Traceback (most recent call last):
File "D:\Programming\Python\Jupyter Notebooks\Python_Programming_Notes\temp.py", line 2, in <module>
print(d1.pop(4))
KeyError: 4
IN [30]
d1 = {1: 12, 2: 23, 3: 34}
print(d1.pop(4, 30))
d1
stdout
30
Removes and returns the last key-value pair of the dictionary.
Syntax:
dict.popitem()
IN [32]
d1 = {1: 12, 2: 23, 3: 34}
print(d1.popitem())
d1
stdout
(3, 34)
Sets the default value to key if the key is absent.
Syntax:
dict.setdefault(key, value)
IN [43]
d1 = {1: 12, 2: 23, 3: 34}
d1.setdefault(4, 45)
d1
The values in the dictionary are accessed by key.
Syntax:
dictionary[key]
IN [33]
d1 = {1: 12, 2: 23, 3: 34}
d1[2]
IN [35]
d1 = {1: 12, 22: 23, 3: 34}
d1[22]
IN [36]
d1 = {1: 12, 2: 23, 3: 34}
d1[4]
Error
Traceback (most recent call last):
File "D:\Programming\Python\Jupyter Notebooks\Python_Programming_Notes\temp.py", line 2, in <module>
d1[4]
KeyError: 4
Items can be added or modified by using keys.
If the key already exists, the value is replcaed by the new value. If not, new key-value pair is created
Syntax:
dictaionary[key] = value
IN [38]
d1 = {1: 12, 2: 23, 3: 34}
d1[2] = 24 # modify
d1[4] = 45 # add
d1
Elements can be deleted by using the del keyword.
Syntax:
del dict[key]
IN [40]
d1 = {1: 12, 2: 23, 3: 34}
del d1[2], d1[3]
d1
Use for loop to iterate through the dictionary.
IN [45]
d1 = {1: 12, 2: 23, 3: 34}
for key, val in d1.items():
print(key, val)
stdout
1 12
2 23
3 34
Dictionaries are sorted only by keys.
IN [51]
d1 = {1: 12, 2: 23, 3: 34}
for i in sorted(d1.keys()):
print(i, d1[i])
print()
for i in sorted(d1):
print(i, d1[i])
stdout
1 12
2 23
3 34
1 12
2 23
3 34
Dictionaries inside a dictionary is nested dictionary.
IN [52]
d1 = {1: 12, 2: 23, 'd': {1: 12, 2: 23, 3: 34}}
for i,j in d1.items():
print(i,j)
stdout
1 12
2 23
d {1: 12, 2: 23, 3: 34}
Dictionaries can be defined by comprehensions.
Syntax:
dict = {key: value (loops)}
IN [53]
d2 = {x: x**2 for x in range(10)}
d2
Dictionaries can be used as switch cases.
IN [55]
switch = {1: 'One',
2: 'Two',
3: 'Three'}
switch[1]