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)
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'])
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)
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)
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)
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])
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)
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)
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)
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))
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)
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)
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)
Example – It deletes whole dictionary by default.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} del dict1 print(dict1)
clear() method – To empty whole dictionary.
Example – Use clear() method to empty whole dictionary.
dict1 = {'name' : 'Alex' , 'age' : 12 , 'add' : 'NYC'} print(dict1.clear())
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)
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)
If you find anything incorrect in above discussed topic and have any further question, please comment down below.
Like us on