Dictionary Methods in Python Program

PythonServer Side ProgrammingProgramming

A python dictionary is a collection data type which is wrapped in braces, {}, with a series of key value pairs inside the braces. Each key is connected to a value. We use a key to access the value associated with that key. A key can be a number, a string, a list, or even another dictionary.

Dictionary Methods

There are many in-built methods available in the python Standard Library which is useful in dictionary operations. Below we will see the examples of most frequently used dictionary methods.

keys()

The method keys() returns a list of all the available keys in the dictionary.

Example

 Live Demo

dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97}
print(dict.keys())

Running the above code gives us the following result

Output

dict_keys(['Name', 'Rollno', 'Dept', 'Marks'])

items()

This method returns a list of dictionary's (key, value) as tuple.

Example

 Live Demo

dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97}
print(dict.items())

Running the above code gives us the following result

Output

dict_items([('Name', 'Harry'), ('Rollno', 30), ('Dept', 'cse'), ('Marks', 97)])

values()

This method returns list of dictionary dictionary's values from the key value pairs.

Example

 Live Demo

dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97}
print(dict.values())

Running the above code gives us the following result:

Output

dict_values(['Harry', 30, 'cse', 97])

pop()

The method pop(key) Removes and returns the value of specified key.

Example

 Live Demo

dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97}
dict.pop('Marks')
print(dict)

Running the above code gives us the following result:

Output

{'Name': 'Harry', 'Rollno': 30, 'Dept': 'cse'}

copy()

This method Returns a shallow copy of dictionary.

Example

 Live Demo

dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97}
dict_new=dict.copy()
print(dict_new)

Running the above code gives us the following result:

Output

{'Name': 'Harry', 'Rollno': 30, 'Dept': 'cse', 'Marks': 97}

clear()

The method clear() Removes all elements of dictionary.

Example

 Live Demo

dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97}
dict.clear()
print(dict)

Running the above code gives us the following result:

Output

{}

get()

This method returns value of given key or None as default if key is not in the dictionary.

Example

 Live Demo

dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97}
print('\nName: ', dict.get('Name'))
print('\nAge: ', dict.get('Age'))

Running the above code gives us the following result:

Output

Name: Harry
Age: None

update()

The update() inserts new item to the dictionary.

Example

 Live Demo

dict={'Name':'Harry','Rollno':30,'Dept':'cse','Marks':97}
dict.update({'Age':22})
print(dict)

Running the above code gives us the following result

Output

{'Name': 'Harry', 'Rollno': 30, 'Dept': 'cse', 'Marks': 97, 'Age': 22}
raja
Published on 23-Dec-2019 10:41:00
Advertisements