Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created July 11, 2020 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/32391a665b4bd2736215cad5d9c5e907 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/32391a665b4bd2736215cad5d9c5e907 to your computer and use it in GitHub Desktop.
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