Skip to content

Instantly share code, notes, and snippets.

@suryasr007
Last active May 7, 2018 11:24
Show Gist options
  • Select an option

  • Save suryasr007/8e8633831729a3ec765dafa399a48d7b to your computer and use it in GitHub Desktop.

Select an option

Save suryasr007/8e8633831729a3ec765dafa399a48d7b to your computer and use it in GitHub Desktop.
# Defining a dictionary
>>> tel = {'jack': 4098, 'sape': 4139}
# If dict keys are only string, we can define as below
>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'jack': 4098, 'guido': 4127}
# Appending a new key value pair to the `tel` dict
>>> tel['guido'] = 4127
# Display items in dictionary
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
# Delete a item from `tel` dict
>>> del tel['sape']
# Represent all the keys of the `tel` in a list
>>> list(tel.keys())
['irv', 'guido', 'jack']
# sort the keys
>>> sorted(tel.keys())
['guido', 'irv', 'jack']
# Check whether a key exists in the `tel` dict
>>> 'guido' in tel
True
>>> 'jack' not in tel
False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment