Dictionary functions in python with examples
There are various types of Dictionary functions in python with examples dictionary.
#1 len()
Dictionary functions in python len() is used to find length of dictionary.
d = { ‘Name’ : ‘Deepak’, ‘city’ : ‘Indore’, ‘Hobby’ : ‘Cricket’ }
print( len(d) )
Output
3
#2 del()
If we want to delete any element from dictionary we use del() function.
d = { ‘Name’ : ‘Deepak’, ‘city’ : ‘Indore’, ‘Hobby’ : ‘Cricket’ }
del d[ ‘city’ ]
print( d )
Output
{ ‘Name’ : ‘Deepak’, ‘Hobby’ : ‘Cricket’ }
#3 sorted
Dictionary functions in python sorted() is used to arrange the dictionary key in sequence. The sorted() function does not affect the original dictionary.
d = { ‘Name’ : ‘Deepak’, ‘city’ : ‘Indore’, ‘Hobby’ : ‘Cricket’ }
print( sorted(d) )
Output
{ ‘city’ : ‘Indore’, ‘Hobby’ : ‘Cricket’, ‘Name’ : ‘Deepak’ }
#4 keys()
keys() function is used to get all keys from dictionary.
d = { ‘Name’ : ‘Deepak’, ‘city’ : ‘Indore’, ‘Hobby’ : ‘Cricket’ }
print( keys(d) )
Output
#5 values()
d = { ‘Name’ : ‘Deepak’, ‘city’ : ‘Indore’, ‘Hobby’ : ‘Cricket’ }
print( d.values() )
Output
#6 items()
The items() functions return the list of key-value pair of dictionary.
d = { ‘Name’ : ‘Deepak’, ‘city’ : ‘Indore’, ‘Hobby’ : ‘Cricket’ }
print( d.items() )
Output
#7 clear()
d = { ‘Name’ : ‘Deepak’, ‘city’ : ‘Indore’, ‘Hobby’ : ‘Cricket’ }
d.clear()
print( d )
Output
Important Links: Informatics Practices
What is Python Dictionary
How to create Python Dictionary
How to Traverse Dictionary in Python
What is Python List
Characteristics of Python List
How to create Python List
How to Traverse a List in Python
How to Manipulate List in Python
How to Modify multiple elements in Python List
Slicing in Python List
How to delete elements from List in Python
If you like my post please share it with your friends by simply clicking the below social media button, and Don’t forget to comment so we will update ourselves.
[sm-youtube-subscribe]