Dictionary Methods in Python | Set 1 (cmp(), len(), items()...) - GeeksforGeeks

28 Jun, 2021
Python Dictionary Basics have been discussed in the article below
Dictionary
Some dictionary methods are discussed in this article.
1. str(dic) :- This method is used to return the string, denoting all the dictionary keys with their values.
2. items() :- This method is used to return the list with all dictionary keys with values.
# Python code to demonstrate working of# str() and items()  # Initializing dictionarydic = { 'Name' : 'Nandini', 'Age' : 19 }  # using str() to display dic as stringprint ("The constituents of dictionary as string are : ")print (str(dic))  # using str() to display dic as listprint ("The constituents of dictionary as list are : ")print (dic.items())
Output:
The constituents of dictionary as string are : 
{'Name': 'Nandini', 'Age': 19}
The constituents of dictionary as list are : 
dict_items([('Name', 'Nandini'), ('Age', 19)])

3. len() :- It returns the count of key entities of the dictionary elements.
4. type() :- This function returns the data type of the argument.
# Python code to demonstrate working of# len() and type()  # Initializing dictionarydic = { 'Name' : 'Nandini', 'Age' : 19, 'ID' : 2541997 }  # Initializing listli = [ 1, 3, 5, 6 ]  # using len() to display dic sizeprint ("The size of dic is : ",end="")print (len(dic))  # using type() to display data typeprint ("The data type of dic is : ",end="")print (type(dic))  # using type() to display data typeprint ("The data type of li is : ",end="")print (type(li))
Output:
The size of dic is : 3
The data type of dic is : 
The data type of li is : 

5. copy() :- This function creates the shallow copy of the dictionary into other dictionary.
6. clear() :- This function is used to clear the dictionary contents.
# Python code to demonstrate working of# clear() and copy()  # Initializing dictionarydic1 = { 'Name' : 'Nandini', 'Age' : 19 }  # Initializing dictionary dic3 = {}  # using copy() to make shallow copy of dictionarydic3 = dic1.copy()  # printing new dictionaryprint ("The new copied dictionary is : ")print (dic3.items())  # clearing the dictionarydic1.clear()  # printing cleared dictionaryprint ("The contents of deleted dictionary is : ",end="")print (dic1.items())
Output:
The new copied dictionary is : 
dict_items([('Age', 19), ('Name', 'Nandini')])
The contents of deleted dictionary is : dict_items([])

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
python-dict
Python
School Programming
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Enumerate() in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
Create a Pandas DataFrame from Lists
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java
C++ Classes and Objects