Python Dictionary Methods


Method Description
clear() Deletes all the elements from the dictionary.
copy() Creates a duplicate copy of the dictionary.
fromkeys() Creates a dictionary from the listed keys and their value.
get() To get the value of the key.
items() It will return tuples of all the items inside the dictionary with keys and values.
keys() To get all the keys used inside the dictionary.
pop() Deletes item with the specified key from the dictionary.
popitems() Removes and returns the last key-value pair from the dictionary.
setdefaults() Set and return the dictionary key with the default value.
update() Updates the existing key with the new value.
values() Returns values of all the keys.

Python Dictionary clear() Method

Deletes all the elements from the dictionary.

In [1]:
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
Out[1]:
{'first_name': 'Tim',
 'last_name': 'Smith',
 'emp_id': 200,
 'programming_languages': ['Python', 'C++', 'Java']}
In [2]:
emp_details.clear()
emp_details
Out[2]:
{}

Python Dictionary copy() Method

Creates a duplicate copy of the dictionary.

In [1]:
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
Out[1]:
{'first_name': 'Tim',
 'last_name': 'Smith',
 'emp_id': 200,
 'programming_languages': ['Python', 'C++', 'Java']}
In [2]:
emp_details_copy = emp_details.copy()
emp_details_copy
Out[2]:
{'first_name': 'Tim',
 'last_name': 'Smith',
 'emp_id': 200,
 'programming_languages': ['Python', 'C++', 'Java']}

Python Dictionary fromkeys() Method

Creates a dictionary from the listed keys and their value.

In [1]:
key = ('price1', 'price2', 'price3')
value = (1200)
In [2]:
prices = dict.fromkeys(key,value)
In [3]:
prices
Out[3]:
{'price1': 1200, 'price2': 1200, 'price3': 1200}

Python Dictionary get() Method

To get the value of the key. If the key is not found in the dictionary, we can return a default value.

In [1]:
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
Out[1]:
{'first_name': 'Tim',
 'last_name': 'Smith',
 'emp_id': 200,
 'programming_languages': ['Python', 'C++', 'Java']}

Trying to get the value of last_name.

In [2]:
emp_details.get('last_name')
Out[2]:
'Smith'

If the key is not available, then it does not return anything.

In [3]:
emp_details.get('middle_name')

We can set the default value that we want to return in case the key is not available.

In [4]:
emp_details.get('middle_name','N/A')
Out[4]:
'N/A'

Python Dictionary items() Method

It will return tuples of all the items inside the dictionary with keys and values.

In [1]:
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details.items()
Out[1]:
dict_items([('first_name', 'Tim'), ('last_name', 'Smith'), ('emp_id', 200), ('programming_languages', ['Python', 'C++', 'Java'])])

Python Dictionary keys() Method

To get all the keys used inside the dictionary.

In [1]:
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details.keys()
Out[1]:
dict_keys(['first_name', 'last_name', 'emp_id', 'programming_languages'])

Python Dictionary pop() Method

Deletes item with the specified key from the dictionary.

In [1]:
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
Out[1]:
{'first_name': 'Tim',
 'last_name': 'Smith',
 'emp_id': 200,
 'programming_languages': ['Python', 'C++', 'Java']}

Removed ‘last_name’ from dictionary.

In [2]:
emp_details.pop('last_name')
emp_details
Out[2]:
{'first_name': 'Tim',
 'emp_id': 200,
 'programming_languages': ['Python', 'C++', 'Java']}

Throws a an error if key is not found.

In [3]:
emp_details.pop('middle_name')
emp_details
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-3-5ea7df2d8765> in <module>()
----> 1 emp_details.pop('middle_name')
      2 emp_details

KeyError: 'middle_name'

Python Dictionary popitems() Method

Removes and returns the last key-value pair from the dictionary.

In [1]:
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
Out[1]:
{'first_name': 'Tim',
 'last_name': 'Smith',
 'emp_id': 200,
 'programming_languages': ['Python', 'C++', 'Java']}
In [2]:
emp_details.popitem()
Out[2]:
('programming_languages', ['Python', 'C++', 'Java'])
In [3]:
emp_details
Out[3]:
{'first_name': 'Tim', 'last_name': 'Smith', 'emp_id': 200}

Python Dictionary setdefaults() Method

Set and return the dictionary key with the default value. If the key is not present in the dictionary, will create new key value pair.

In [1]:
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
Out[1]:
{'first_name': 'Tim',
 'last_name': 'Smith',
 'emp_id': 200,
 'programming_languages': ['Python', 'C++', 'Java']}

Returns the existing value as the key is already available.

In [2]:
value = emp_details.setdefault('last_name','Wills')
value
Out[2]:
'Smith'

Returns the new value as the key is not available.

In [3]:
value = emp_details.setdefault('middle_name','Wills')
value
Out[3]:
'Wills'

Observe that last_name is still the same but a new key:value pair for middle_name is added.

In [4]:
emp_details
Out[4]:
{'first_name': 'Tim',
 'last_name': 'Smith',
 'emp_id': 200,
 'programming_languages': ['Python', 'C++', 'Java'],
 'middle_name': 'Wills'}

Python Dictionary update() Method

Updates the existing key with the new value. If the key does not exist, it will create a new key:value pair.

In [1]:
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details
Out[1]:
{'first_name': 'Tim',
 'last_name': 'Smith',
 'emp_id': 200,
 'programming_languages': ['Python', 'C++', 'Java']}

‘last_name’ got updated.

In [2]:
emp_details.update({'last_name':'Wills'})
emp_details
Out[2]:
{'first_name': 'Tim',
 'last_name': 'Wills',
 'emp_id': 200,
 'programming_languages': ['Python', 'C++', 'Java']}

‘middle_name’ got added.

In [3]:
emp_details.update({'middle_name':'David'})
emp_details
Out[3]:
{'first_name': 'Tim',
 'last_name': 'Wills',
 'emp_id': 200,
 'programming_languages': ['Python', 'C++', 'Java'],
 'middle_name': 'David'}

Python Dictionary values() Method

Returns values of all the keys.

In [1]:
emp_details = {'first_name':'Tim', 'last_name':'Smith', 'emp_id':200, 'programming_languages':['Python','C++','Java']}
emp_details.values()
Out[1]:
dict_values(['Tim', 'Smith', 200, ['Python', 'C++', 'Java']])