Dictionary in Python - some more functions and methods.docx
Dictionary in Python - some more functions and methods.docx
fromkeys()
The fromkeys() method is used to create a new dictionary from a
sequence containing all keys and a common value, which will be
assigned to all the keys.
If you specify the key sequence and do not give any value, it will take
None as the values for the keys.
If you specify a sequence of values in place of the value argument, it
will consider the whole sequence as the value for the keys.
If you have a single element , then you should give it in tuple or list
form.
sorted()
D1 = sorted(D)
It takes the dictionary as parameter and creates a new list with key
values in sorted order
There is no change in D.
del
del D[1] It deletes the record with key value 1.
del(D) It deletes the entire dictionary D.
popitem() pop()
D.popitem() It removes the element on the right side.
D.pop(2) It removes the element with key value 2.
copy()
D1 = D.copy()
It makes the copy of dictionary D in D1 and both have different
address (ids)
Note:This function is available in mutable objects.