Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created Jul 11, 2020
Embed
What would you like to do?
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