Dictionary Methods ( Part 1 )
Last Updated :
14 Jul, 2022
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.
dic = { 'Name' : 'Nandini' , 'Age' : 19 }
print ( "The constituents of dictionary as string are : " )
print ( str (dic))
print ( "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.
dic = { 'Name' : 'Nandini' , 'Age' : 19 , 'ID' : 2541997 }
li = [ 1 , 3 , 5 , 6 ]
print ( "The size of dic is : " ,end = "")
print ( len (dic))
print ( "The data type of dic is : " ,end = "")
print ( type (dic))
print ( "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.
dic1 = { 'Name' : 'Nandini' , 'Age' : 19 }
dic3 = {}
dic3 = dic1.copy()
print ( "The new copied dictionary is : " )
print (dic3.items())
dic1.clear()
print ( "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([])
Share your thoughts in the comments
Please Login to comment...