Skip to content

Instantly share code, notes, and snippets.

@GeorgeSeif
Created October 31, 2018 03:08
Show Gist options
  • Select an option

  • Save GeorgeSeif/8f20443ab22db44520d86d4a1ee8d41a to your computer and use it in GitHub Desktop.

Select an option

Save GeorgeSeif/8f20443ab22db44520d86d4a1ee8d41a to your computer and use it in GitHub Desktop.
# Data is stored in a dictionary as key-values pairs
my_dict = {
"bob": "bob1991@gmail.com",
"claire": "claire1986@gmail.com",
"john": "john_22@gmail.com"
}
# You can initialize an empty dictionary in 2 ways
my_dict = {}
my_dict = dict()
# You access the values inside via the key
my_dict["claire"] # Outputs "claire1986@gmail.com"
# Add items to your dictionary like so
my_dict = dict()
my_dict["bob"] = "bob1991@gmail.com"
my_dict["claire"] = "claire1986@gmail.com"
my_dict["john"] = "john_22@gmail.com"
# We can also loop through the keys and values
# The following code prints each key and value of my_dict
for key, value in dictionamy_dictry.items():
print("key: {0}, value:{1}".format(key, value))
# If you want to delete an item from a dictionary, be sure
# to do it OUTSIDE of a loop. Dictionaries can't be modified
# inside a loop, it's Python law
del my_dict["john"]
# Get a list of all the keys or values in the dictionary
my_dict.keys()
my_dict.values()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment