items():-
items() method डिक्शनरी के सभी key-value pairs को tuple में return करता है (represent list of tuple)
dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}
print(dict1.items()) # dict_items([(‘one’, ‘Ramesh’), (‘two’, ‘suresh’), (‘three’, ‘rajesh’)])
update():-
update() method का इस्तेमाल old dictionary को update करने के लिए किया जाता है
dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}
print(“Before Updating or Adding Keys and Values :”)
print(dict1) # {‘one’: ‘Ramesh’, ‘two’: ‘suresh’, ‘three’: ‘rajesh’}
dict2 = {“one”:’akhilesh’, “four”:’rakesh’, “five”:’mahesh’}
print(“After Updating or Adding Keys and Values:”)
dict1.update(dict2)
print(dict1) # {‘one’: ‘akhilesh’, ‘two’: ‘suresh’, ‘three’: ‘rajesh’, ‘four’: ‘rakesh’, ‘five’: ‘mahesh’}
fromkeys():-
fromkeys() method दिए हुए sequence के item को dictionary के key के रूप में return करता है
Syntax:- dict.fromkeys(seq, val)
Parameter :
seq : यहाँ पर sequence(list, tuple, string) दिया जाता है। return होने वाले नए dictionary के लिए ये ‘keys’ होती है।
val : Optional. ये keys की values होती है। अगर दी नहीं जाती है तो ‘None’ return होता है।
list = [“H”, “e”, “l”, “l”, “o”]
print(dict.fromkeys(list)) # {‘H’: None, ‘e’: None, ‘l’: None, ‘o’: None}
Note:- अगर दूसरा parameter ‘seq’ दिया नहीं जाता है तो ‘None’ return होता है
list = [“H”, “e”, “l”, “l”, “o”]
print(dict.fromkeys(list, “uma”)) # {‘H’: ‘uma’, ‘e’: ‘uma’, ‘l’: ‘uma’, ‘o’: ‘uma’}
items():-
items() method returns all key-value pairs of the dictionary in a tuple (representing list of tuple)
dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}
print(dict1.items()) # dict_items([(‘one’, ‘Ramesh’), (‘two’, ‘suresh’), (‘three’, ‘rajesh’)])
update():-
update() method is used to update the old dictionary.
dict1 = {“one”:’Ramesh’, “two”:’suresh’, “three”:’rajesh’}
print(“Before Updating or Adding Keys and Values :”)
print(dict1) # {‘one’: ‘Ramesh’, ‘two’: ‘suresh’, ‘three’: ‘rajesh’}
dict2 = {“one”:’akhilesh’, “four”:’rakesh’, “five”:’mahesh’}
print(“After Updating or Adding Keys and Values:”)
dict1.update(dict2)
print(dict1) # {‘one’: ‘akhilesh’, ‘two’: ‘suresh’, ‘three’: ‘rajesh’, ‘four’: ‘rakesh’, ‘five’: ‘mahesh’}
fromkeys():-
fromkeys() method returns the items of the given sequence as keys of the dictionary.
Syntax:- dict.fromkeys(seq, val)
Parameter :
seq : Here sequence(list, tuple, string) is given. These are ‘keys’ for the new dictionary to be returned.
val : Optional. These are the values of the keys. If not given then ‘None’ is returned.
list = [“H”, “e”, “l”, “l”, “o”]
print(dict.fromkeys(list)) # {‘H’: None, ‘e’: None, ‘l’: None, ‘o’: None}
Note:- If second parameter ‘seq’ is not given then ‘None’ is returned.
list = [“H”, “e”, “l”, “l”, “o”]
print(dict.fromkeys(list, “uma”)) # {‘H’: ‘uma’, ‘e’: ‘uma’, ‘l’: ‘uma’, ‘o’: ‘uma’}