In this article you will learn about Python dictionary Methods
Python Dictionary – Before moving ahead, let’s take a look at another Python collection – Python Set
Dictionary – It is collection of unordered, indexed and changeable elements. Dictionary used curly brackets to store key:value pair elements, separated with comma after each key:value pair.
- Left side is defined as key and right side is value.
- Every key and value are separated with comma sign.
Example – Creation of dictionary.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} print(dict1)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-263.png?resize=907.5%2C499&ssl=1)
Access elements – There are two ways to access elements.
1. By referring specified key name.
Example – It returns the value of specified key.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} print(dict1['age'])
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-264.png?resize=907.5%2C495&ssl=1)
2. get() method – By referring specified key name.
Example – It returns the value of specified key.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} x = dict1.get('age') print(x)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-265.png?resize=907.5%2C498&ssl=1)
Change elements – A element of dictionary can be changed by referring its key name.
Example – It returns the change value of key name ‘age’.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} dict1['age'] = 15 print(dict1)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-266.png?resize=907.5%2C497&ssl=1)
loop through a Dictionary – It is possible to loop through dictionary using ‘for’ loop. As output it returns key names. But, also possible to return values.
Example – It returns key names of dictionary through ‘for’ loop.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} for x in dict1: print(x)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-267.png?resize=907.5%2C493&ssl=1)
Example – It returns value names of dictionary key through ‘for’ loop.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} for x in dict1: print(dict1[x])
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-268.png?resize=907.5%2C498&ssl=1)
values() method – It returns the value of the dictionary.
Example – It returns value names of dictionary by using value() method.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} for x in dict1.values(): print(x)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-269.png?resize=907.5%2C486&ssl=1)
items() method – By using this method we can loop through both keys and values of dictionary.
Example – It returns both keys and values by items() method.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} for x , y in dict1.items(): print(x , y)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-270.png?resize=907.5%2C477&ssl=1)
Check if key exists– To know particular key name is present in dictionary use ‘in’ keyword.
Example – Use ‘in’ keyword to determine particular key name.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} print('add' in dict1)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-271.png?resize=907.5%2C485&ssl=1)
len() method – Use len() method to get the length of element (key-value pairs) the dictionary.
Example – Use len() method to get the length of the dictionary.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} print('The length of dictionary is: ' , len(dict1))
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-272.png?resize=827%2C446&ssl=1)
Add elements – Adding an element in dictionary can be done by referring an index key and assigning value to it.
Example – Adding an element in the dictionary.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} dict1['ID'] = 458 print(dict1)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-273.png?resize=907.5%2C494&ssl=1)
Remove elements – Dictionary has four methods to remove its elements (key- value pairs).
1. pop() method – It removes specified key name. By default it removes last element.
Example 12- It removes specified key name to given dictionary.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} x = dict1.pop('add') print(x)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-274.png?resize=907.5%2C498&ssl=1)
2. del method – It removes the specified key name. By default it deletes whole dictionary.
Example 13- It deletes the specified key name.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} del dict1['name'] print(dict1)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-275.png?resize=907.5%2C490&ssl=1)
Example – It deletes whole dictionary by default.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} del dict1 print(dict1)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-276.png?resize=907.5%2C500&ssl=1)
clear() method – To empty whole dictionary.
Example – Use clear() method to empty whole dictionary.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} print(dict1.clear())
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-277.png?resize=907.5%2C476&ssl=1)
copy() method – If you copy a dictionary simply by referring a variable name like dict = dict1 then will not copy a dictionary because dict will be only a reference of dict1. Changes made in dict1 (original dictionary) will automatically also made in dict.
Example – Use copy() method to make a copy of dictionary.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} x = dict1.copy() print(x)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-278.png?resize=907.5%2C482&ssl=1)
dict() method – It returns copy of dictionary.
Example – Use dict() method to make a copy of dictionary.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} x = dict(dict1) print(x)
![python-dictionary](https://i0.wp.com/codingstreets.com/wp-content/uploads/2021/05/image-279.png?resize=907.5%2C474&ssl=1)
If you find anything incorrect in above discussed topic and have any further question, please comment down below.
Like us on