Table of Contents
Dictionary operations and methods in python
Python supports various compound datatypes like dictionary, tuple,string etc.
We have already seen dictionary and its properties.
In this post we will be concentrating on various dictionary methods.
Dictionary consist of various methods for easy functioning and utilization of python script.
Operations in dictionary are traversing and appending new items.
Many methods like keys(), items(), values(), update() etc are used to provide various functionalities and can be used again and again as per need without need of coding again this helps in reducing redundancy.
Operations in dictionary
We have already seen creation of dictionary, initialization, deletion and updating dictionary.
Apart from this dictionary operations we will be seeing other operations like traversing a dictionary and appending new elements or items to dictionary.
List of dictionary operations
- Traversing a dictionary
- It is used to traverse through dictionary elements.
- Membership operators in and not in is used for traversing through the dictionary elements.
- To display all the keys in itv
#to display all the keys in itv
itv={'1':'pink','2':'blue','3':'black'}
print("itv =",itv)
for i in itv:
print(i)
- Output:
>>>
itv = {'2': 'blue', '3': 'black', '1': 'pink'}
2
3
1
- To display key : value pairs from itv
itv={'1':'pink','2':'blue','3':'black'}
print("itv =",itv)
for m,n in itv.items():
print(m,n)
- Output:
itv = {'1': 'pink', '3': 'black', '2': 'blue'}
1 pink
3 black
2 blue
- Appending elements in dictionary
- Appending is adding new value with the existing items of the dictionary
- To append new element we need to use = operator.
itv={'1':'pink','2':'blue','3':'black'}
print("itv =",itv)
#appending a value
itv[4]='yellow'
print("itv =",itv)
- Output:
itv = {'2': 'blue', '3': 'black', '1': 'pink'}
itv = {4: 'yellow', '2': 'blue', '3': 'black', '1': 'pink'}
Methods of dictionary
Dictionary has many built-in methods to support functionalities like fetching keys, values, items, length etc.
List of dictionary methods
len()
- It returns total length of dictionary.
- The length is an integer value of number of items present in the dictionary.
len(dict)
- The parameter dict is the dictionary whose length needs to be calculated.
- clear()
- This methods is used to remove all the items from the dictionary.
- It does not return any value.
dict.clear()
- get()
- This method is used to give the value of given key.
- If the given key is not available then it will return the default value none.
dict.get(key,default=None)
The parameter key is the key which is to be searched in the dictionary.
- update()
- This method is used to add the key values of one dictionary to another dictionary.
- This function does not return anything it just updates teh dictionary
dict1.update(dict2)
- The parameter dict1 will get updated with items present in dict2.
- keys()
- It returns list of all keys present in given dictionary.
dict.keys()
- Above syntax shows that all keys of dict will be returned.
- values()
- It returns list of all values present in given dictionary.
dict.values()
- Above syntax shows that all keys of dict will be returned.
- items()
- It is used to find the key – value pairs and return a tuple of same.
dict.items()
- Above syntax shows that all key:value pairs of dict will be returned.
- pop()
- It is used to remove specified key and return the corresponding value.
- If key is not found, d is returned if given, otherwise KeyError is raised
dict.pop('key')
- popitem()
- It is used to remove key value pair from given dictionary without mentioning key or value as parameter.
- popitem() does not have any parameter.
dict.popitem()
Example of dictionary methods and opertaions:

Output of dictionary methods in python
>>>
sorted itv by using keys [1, 2, 3, 4, 5]
square is {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
after del square[4] {1: 1, 2: 4, 3: 9, 5: 25}
itv after appending itv itv[6]=’six’ {1: ‘one’, 2: ‘two’, 3: ‘three’, 4: ‘four’, 5: ‘five’, 6: ‘six’}
len(itv) 6
itv.get(6) six
itv.items() dict_items([(1, ‘one’), (2, ‘two’), (3, ‘three’), (4, ‘four’), (5, ‘five’), (6, ‘six’)])
itv.values() dict_values([‘one’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’])
after itv.pop(2) {1: ‘one’, 3: ‘three’, 4: ‘four’, 5: ‘five’, 6: ‘six’}
after itv.popitem() {3: ‘three’, 4: ‘four’, 5: ‘five’, 6: ‘six’}
after itv.clear() {}
>>>
>>>
Other python related post
Other advance python related post
