-
-
Save IndhumathyChelliah/32391a665b4bd2736215cad5d9c5e907 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d1 = {'name': 'karthi', | |
'age': 7, | |
'city': 'chennai', | |
} | |
# popitem | |
# The popitem() method removes and returns the last element (key, value) pair inserted into the dictionary. | |
print(d1.popitem()) # Output:('city', 'chennai') | |
print (type(d1.popitem()))#Output:<class 'tuple'> | |
print(d1) # Output:{'name': 'karthi', 'age': 7} | |
# pop | |
# pop() method removes and returns a value from a dictionary for the given key. | |
print(d1.pop('name')) # Output:Karthi | |
print(d1) # Output: {'age': 7} | |
# If key is not in dictionary and value is given as default argument means,it will return the default value. | |
print(d1.pop("city", "Not found")) # Output: Not found | |
# if key is not in dictionary and default value not given means it will raise KeyError | |
# print (d1.pop('rollno')) #Output: KeyError: 'rollno' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment