Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Created July 10, 2020 23:40
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/f742ebf86aa63622b74c27d20ca98ca5 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/f742ebf86aa63622b74c27d20ca98ca5 to your computer and use it in GitHub Desktop.
d = {'name': 'karthi',
'age': 7,
'city': 'chennai',
}
# If key is in dictionary means, it is removed from the dictionary.
del d['name']
print(d) # Output: {'age': 7, 'city': 'chennai'}
# If key is not in dictionary means KeyError is thrown.
del d['state']# Output:KeyError: 'state'
# clear - it will empty the dictionary.
d.clear()
# clear() method will return None.It will clear the original dictionary
print(d.clear()) # Output: None.
print(d) # Output: {}
# del - del keyword is used to delete the dictionary itself.
del d
# d1 is deleted and is not available.
print (d) #Output: NameError: name 'd' is not defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment