Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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()
Add Comment
Please, Sign In to add comment