Guest User

Untitled

a guest
Oct 31st, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. # Data is stored in a dictionary as key-values pairs
  2. my_dict = {
  3. "bob": "bob1991@gmail.com",
  4. "claire": "claire1986@gmail.com",
  5. "john": "john_22@gmail.com"
  6. }
  7.  
  8. # You can initialize an empty dictionary in 2 ways
  9. my_dict = {}
  10. my_dict = dict()
  11.  
  12. # You access the values inside via the key
  13. my_dict["claire"] # Outputs "claire1986@gmail.com"
  14.  
  15. # Add items to your dictionary like so
  16. my_dict = dict()
  17. my_dict["bob"] = "bob1991@gmail.com"
  18. my_dict["claire"] = "claire1986@gmail.com"
  19. my_dict["john"] = "john_22@gmail.com"
  20.  
  21. # We can also loop through the keys and values
  22. # The following code prints each key and value of my_dict
  23. for key, value in dictionamy_dictry.items():
  24. print("key: {0}, value:{1}".format(key, value))
  25.  
  26. # If you want to delete an item from a dictionary, be sure
  27. # to do it OUTSIDE of a loop. Dictionaries can't be modified
  28. # inside a loop, it's Python law
  29. del my_dict["john"]
  30.  
  31. # Get a list of all the keys or values in the dictionary
  32. my_dict.keys()
  33. my_dict.values()
Add Comment
Please, Sign In to add comment