Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. # DICTIONARIES
  2.  
  3. * properties: unordered, iterable, mutable, can contain multiple data types
  4. * made up of key-value pairs
  5. * keys must be unique, and can be strings, numbers, or tuples
  6. * values can be any type
  7.  
  8. ## create an empty dictionary (two ways)
  9. ```python
  10. empty_dict = {}
  11. empty_dict = dict()
  12. ```
  13.  
  14. ## create a dictionary (two ways)
  15. ```python
  16. family = {'dad':'homer', 'mom':'marge', 'size':6}
  17. family = dict(dad='homer', mom='marge', size=6)
  18. ```
  19.  
  20. ## convert a list of tuples into a dictionary
  21. ```python
  22. list_of_tuples = [('dad','homer'), ('mom','marge'), ('size', 6)]
  23. family = dict(list_of_tuples)
  24. ```
  25.  
  26. ## examine a dictionary
  27. ```python
  28. family['dad'] # returns 'homer'
  29. len(family) # returns 3
  30. family.keys() # returns list: ['dad', 'mom', 'size']
  31. family.values() # returns list: ['homer', 'marge', 6]
  32. family.items() # returns list of tuples:
  33. # [('dad', 'homer'), ('mom', 'marge'), ('size', 6)]
  34. 'mom' in family # returns True
  35. 'marge' in family # returns False (only checks keys)
  36. ```
  37.  
  38. ## modify a dictionary (does not return the dictionary)
  39. ```python
  40. family['cat'] = 'snowball' # add a new entry
  41. family['cat'] = 'snowball ii' # edit an existing entry
  42. del family['cat'] # delete an entry
  43. family['kids'] = ['bart', 'lisa'] # value can be a list
  44. family.pop('dad') # removes an entry and returns the value ('homer')
  45. family.update({'baby':'maggie', 'grandpa':'abe'}) # add multiple entries
  46. ```
  47.  
  48. ## accessing values more safely with 'get'
  49. ```python
  50. family['mom'] # returns 'marge'
  51. family.get('mom') # same thing
  52. family['grandma'] # throws an error
  53. family.get('grandma') # returns None
  54. family.get('grandma', 'not found') # returns 'not found' (the default)
  55. ```
  56.  
  57. ## accessing a list element within a dictionary
  58. ```python
  59. family['kids'][0] # returns 'bart'
  60. family['kids'].remove('lisa') # removes 'lisa'
  61. ```
  62.  
  63. ## string substitution using a dictionary
  64. ```python
  65. 'youngest child is %(baby)s' % family # returns 'youngest child is maggie'
  66. ```
  67.  
  68. ## Create a dictionary
  69. ```python
  70. dict = {'county': ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'],
  71. 'year': [2012, 2012, 2013, 2014, 2014],
  72. 'fireReports': [4, 24, 31, 2, 3]}
  73. ```
  74.  
  75. ## Create a list from the dictionary keys
  76.  
  77. ## Create a list to place the dictionary keys in
  78. ```python
  79. dictionaryKeys = []
  80. ```
  81.  
  82. ## For each key in the dictionary's keys,
  83. ```python
  84. for key in dict.keys():
  85. # add the key to dictionaryKeys
  86. dictionaryKeys.append(key)
  87. ```
  88.  
  89. ## View the dictionaryKeys list
  90. ```python
  91. dictionaryKeys
  92. ```
  93.  
  94. ## equivalent list comprehension
  95. ```python
  96. items = [item for row in matrix
  97. for item in row] # [1, 2, 3, 4]
  98. ```
  99.  
  100. ## set comprehension
  101. ```python
  102. fruits = ['apple', 'banana', 'cherry']
  103. unique_lengths = {len(fruit) for fruit in fruits} # {5, 6}
  104. ```
  105.  
  106. ## dictionary comprehension
  107. ```python
  108. fruit_lengths = {fruit:len(fruit) for fruit in fruits} # {'apple': 5, 'banana': 6, 'cherry': 6}
  109. fruit_indices = {fruit:index for index, fruit in enumerate(fruits)} # {'apple': 0, 'banana': 1, 'cherry': 2}
  110. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement