☞ Following are the functions of tuple :
1. len() 2. clear() 3. get() 4. items() 5. keys() 6. values() 7. copy() 8. fromkeys() 9. setdefault() 10. pop() 11. popitem() 12. update() 13. max() 14. min() 15. sorted()
☞len() :
len(<dictionary>)
D = {10:"Ten", 20:"Twenty", 30:"Thirty"}
print (len(D))
3
☞clear() :
<dictionary>.clear()
D = {10:"Ten", 20:"Twenty", 30:"Thirty"}
D.clear()
print (D)
{}
☞get() :
<dictionary>.get(<key>, default=None)
D = {10:"Ten", 20:"Twenty", 30:"Thirty"}
a = D.get(20)
print (a)
b = D.get(50) #it will give None
print (b)
Twenty None
D = {10:"Ten", 20:"Twenty", 30:"Thirty"}
a = D.get(20, "hello")
print (a)
b = D.get(50, "hello") #it will default value-hello
print (b)
Twenty hello
☞items() :
<dictionary>.items()
D = {10:"Ten", 20:"Twenty", 30:"Thirty"}
print(D.items())
dict_items([(10, 'Ten'), (20, 'Twenty'), (30, 'Thirty')])
☞keys() :
<dictionary>.keys()
D = {10:"Ten", 20:"Twenty", 30:"Thirty"}
print(D.keys())
dict_keys([10, 20, 30])
☞values() :
<dictionary>.values()
D = {10:"Ten", 20:"Twenty", 30:"Thirty"}
print(D.values())
dict_values(['Ten', 'Twenty', 'Thirty'])
☞copy() :
<dictionary_new> = <dictionary_old>.copy()
D1= {10:"Ten", 20:"Twenty", 30:"Thirty"}
D2 = D1.copy()
print(D1, id(D1))
print(D2, id(D2))
{10: 'Ten', 20: 'Twenty', 30: 'Thirty'} 2287432443264
{10: 'Ten', 20: 'Twenty', 30: 'Thirty'} 2287394766784
☞ If we will use assignment(=) operator, then changes will reflect in both dictionaries.
Syntax :<dictionary_new> = <dictionary_old>
D1= {10:"Ten", 20:"Twenty", 30:"Thirty"}
D2 = D1
print(D1, id(D1))
print(D2, id(D2))
{10: 'Ten', 20: 'Twenty', 30: 'Thirty'} 2215183394176
{10: 'Ten', 20: 'Twenty', 30: 'Thirty'} 2215183394176
☞fromkeys() :
<dictionary>.fromkeys(<collection_of_keys>, <default_value>)
key = [10, 20, 30] value = ["A", "B", "C"] D = dict.fromkeys(key, value) print(D)
{10: ['A', 'B', 'C'], 20: ['A', 'B', 'C'], 30: ['A', 'B', 'C']}
key = [10, 20, 30] D = dict.fromkeys(key) print(D)
{10: None, 20: None, 30: None}
☞setdefault() :
variable_name = <dictionary>.setdefault(<key>, <default_value>)
D = {"Name" : "Avlokan", "Gender" : "Male"}
print(D)
name = D.setdefault("Name", "Not Available")
dob = D.setdefault("Date", "Not Available")
gender = D.setdefault("gender")
print("Name =",name)
print("Date_of_birth =",dob)
print("Gender = ", gender)
{'Name': 'Avlokan', 'Gender': 'Male'}
Name = Avlokan
Date_of_birth = Not Available
Gender = None
☞pop(key) :
<dictionary>.pop(<key>)
D = {"A" : 10, "B" : 20, "C" : 30}
print("Deleted value =", D.pop("B"))
print(D)
Deleted value = 20
{'A': 10, 'C': 30}
☞popitem() :
<dictionary>.popitem()
D = {"A" : 10, "B" : 20, "C" : 30}
print("Deleted item =", D.popitem())
print(D)
Deleted item = ('C', 30)
{'A': 10, 'B': 20}
☞update() :
<dictionary_1>.update(<dictionary_2>)
D1 = { "B" : 10, "C" : 20}
D2 = { "A" : 50, "D" : 30, "B" : 40}
D1.update(D2)
print(D1)
print(D2)
{'B': 40, 'C': 20, 'A': 50, 'D': 30}
{'A': 50, 'D': 30, 'B': 40}
☞min() :
min(<dictionary>) #will give minimum key min(<dictionary>.values()) #will give minimum value
D1 = { "B" : 10, "C" : 20, "A" : 30}
print(min(D1)) #will give mininum key
print(min(D1.values())) #will give mininum value
D2 = { 50 : "abc", 30 : "xyz", 40 : "uvw"}
print(min(D2)) #will give mininum key
print(min(D2.values())) #will give mininum value
A 10 30 abc
☞max() :
max(<dictionary>) #will give maximum key max(<dictionary>.values()) #will give maximum value
D1 = { "B" : 10, "C" : 20, "A" : 30}
print(max(D1)) #will give maximum key
print(max(D1.values())) #will give maximum value
D2 = { 50 : "abc", 30 : "xyz", 40 : "uvw"}
print(max(D2)) #will give maximum key
print(max(D2.values())) #will give maximum value
C 30 50 xyz
☞sorted() :
sorted( <tuple>) sorted( <tuple>, reverse = True)
D1 = { "B" : 10, "C" : 20, "A" : 30}
print(sorted(D1)) #according to key
print(sorted(D1.values())) #according to value
print(sorted(D1.items())) #according to key
print("------------------")
print(sorted(D1, reverse=True))
print(sorted(D1.values(), reverse=True))
print(sorted(D1.items(), reverse=True))
['A', 'B', 'C']
[10, 20, 30]
[('A', 30), ('B', 10), ('C', 20)]
------------------
['C', 'B', 'A']
[30, 20, 10]
[('C', 20), ('B', 10), ('A', 30)]