Python Dictionary Methods

Various methods are provided to process the elements of a dictionary. These methods generally retrieve or manipulate the contents of a dictionary.

Method Example Description
clear() d.clear() Removes all key-value pairs from dictionary 'd'.
copy() d1.d.copy() Copies all elements from 'd' into a new dictionary 'd1'.
fromkeys() d.fromkeys(s[,v]) Create a new dictionary with keys from sequence 's' and values all set to 'v'.
get() d.get(k[,v]) Returns the value associated with key 'k'.If key is not found,it returns 'v'.
items() d.items() Returns an object that contains key-value pairs of 'd'.The pairs are stored as tuples in the object.
keys() d.keys() Returns a sequence of keys from the dictionary 'd'.
values() d.values() Returns a sequence of values from the dictionary 'd'.
update() d.update(x) Adds all elements from dictionary 'x' to 'd'.
pop() d.pop(k[,v]) Removes the key 'k' and its value from 'd' and returns the value.If key is not found,then the value 'v' is returned.If key is not found and 'v' is not mentioned then 'KeyError' is raised.
setdefault() d.setdefault(k[,v]) If key 'k' is found,its value is returned.If key is not found,then the k,v pair is stored into the dictionary 'd'.

A Python program to retrieve keys, values and key-value pairs from a dictionary.

dict = {'Name': 'Chandra', 'Id': 200, 'Salary': 9080.50} 
print('Keys in dict=', dict.keys()) 
print('Values in dict=', dict.values()) 
print('Items in dict=', dict.items())

A Python program to create a dictionary and find the sum of values.

dict = eval(input("Enter elements in {}:")) 
#find the sum of values 
s = sum(dict.values()) 
print('Sum of values in the dictionary:', s)

First we create an empty dictionary 'x'. We enter the key into 'k' and value into 'v' and then using the update() method, we will store these key-value pairs into the dictionary 'x', as shown in the following statement:


A Python program to create a dictionary from keyboard and display the elements.

x = {} 
n = int(input('How many elements?')) 
for i in range(n): 
   k = input('Enter key:')   #key is string 
   v = int(input('Enter its value:')) #value is integer 
print('The dictionary is:', x)

A Python program to create a dictionary with cricket players names and scores in a match. Also we are retrieving runs by entering the player's name.

x = {} 
n = int(input('How many players?')) 
for i in range(n): 
   k = input('Enter player name:') 
   v = int(input('Enter runs:')) 
print('\nPlayers in this match:') 
for pname in x.keys(): #keys() will give only keys 
name = input('Enter player name:') 
runs = x.get(name, -1) 
if(runs == -1): 
   print('Player not found') 
   print('{} made runs {}.'.format(name, runs))

Using for Loop with Dictionaries

For loop is very convenient to retrieve the elements of a dictionary. Let's take a simple dictionary that contains color code and its name as:

colors = {'r': "Red", 'g': "Green", 'b': "Blue", 'w': "White"}
for k in colors: 
   print (k)
for k in colors: 
   print (colors[k])
for k, v in colors.items(): 
   print('Key= {} Value= {}'. format(k, v))