Dictionary
A dictionary is a set of unordered key, value pairs.
In a dictionary, the keys must be unique and they are stored in
an unordered manner.
Creating a Dictionary
The keys would need to be of an immutable type, i.e., data-
types for which the keys cannot be changed at runtime such as
int, string, tuple, etc. The values can be of any type. Individual
pairs will be separated by a comma(“,”) and the whole thing
will be enclosed in curly braces{...}.
Example:
D={2:6, 8:7, “a”:23}
We cannot use indexes in dictionary we write key and access the value if
we try to use index we get key error.
>>> d={2:6,8:7,"a":23}
>>> d[0]
KeyError: 0
Note: values can be repeated but keys should not be repeated.
>>> d={1:2,1:3,2:3}
>>> d
{1: 3, 2: 3}
Creating empty dictionary:
>>> d={}
>>> type(d)
<class 'dict'>
Creating a dictionary using dict() function
>>> d=dict()
>>> print(d)
{}
Assigning vales to dictionary
>>> d[1]="one"
>>> d[2]="two"
>>> d[3]="three"
>>> d[4]="four"
>>> print(d)
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
Accessing values in dictionary
We can access elements in dictionary we use keys
d={1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> print(d[3])
Three
Accessing all keys and values
>>> d={1:5,7:8,8:45}
>>> k=d.keys()
>>> print(k)
dict_keys([1, 7, 8])
>>> v=d.values()
>>> print(v)
dict_values([5, 8, 45])
Note:
In dictionary if we use “+” symbol it adds the values. Similarly if we
use”-“symbol it multiply the values of respective keys.
>>> d1={1:2,2:3,3:4}
>>> d2={4:5,6:7,8:9}
>>> d1+d2
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
>>>>>> d1*d2
TypeError: unsupported operand type(s) for *: 'dict' and 'dict'
>>> d1[2]+d2[4]
>>> d1[2]*d2[4]
15
Dictionary operations
1.Traversing in dictionary:
d1={1:2,2:3,3:4}
for i in d1:
print(i,"-",d1[i])
output:
1-2
2-3
3-4
2. Adding element in a dictionary
>>> d1={1:6,'a':45}
>>> d1[3.2]="hello"
>>> print(d1)
{1: 6, 'a': 45, 3.2: 'hello'}
3.updating existing key in dictionary
>>> d1[1]="python"
>>> print(d1)
{1: 'python', 'a': 45, 3.2: 'hello'}
4.Deleting is done using del keyword and pop () functions
d1= {1: 'python', 'a': 45, 3.2: 'hello', 45: 9}
>>> del d1[1]
>>>print( d1)
{'a': 45, 3.2: 'hello', 45: 9}
>>> d1.pop(45)
>>> print(d1)
{'a': 45, 3.2: 'hello'}
5. in and not in membership operators
Checks whether the given key exist or not
d1={1:2,2:3,3:4}
if 2 in d1:
print("existing")
else:
print("not existing")
output:
existing
Dictionary functions:
1.len():returns length of dictionary
>>> d1={7:6,"a":"python",8:45}
>>> x=len(d1)
>>> print(x)
2.clear(): clears all keys and values from dictionary
>>> d1={7:6,"a":"python",8:45}
>>> d1.clear()
>>> print(d1)
{}
3.get(): returns value of a given key.
>>> d1={7:6,"a":"python",8:45}
>>> v=d1.get(7)
>>> print(v)
4.items():Returns all key values as tuple
>>> d1={7:6,"a":"python",8:45}
>>> d1.items()
dict_items([(7, 6), ('a', 'python'), (8, 45)])
5.keys(): returns a list of keys in a dictionary
>>> d1={7:6,"a":"python",8:45}
>>> d1.keys()
dict_keys([7, 'a', 8])
6.values(): returns list of vales in a dictionary
>>> d1={7:6,"a":"python",8:45}
>>> d1.values()
dict_values([6, 'python', 45])