Creating Lists From Dictionary Keys And Values

Want to learn more? I recommend these Python books: Python for Data Analysis, Python Data Science Handbook, and Introduction to Machine Learning with Python.

Create a dictionary

dict = {'county': ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'],
        'year': [2012, 2012, 2013, 2014, 2014],
        'fireReports': [4, 24, 31, 2, 3]}

Create a list from the dictionary keys

# Create a list to place the dictionary keys in
dictionaryKeys = []

# For each key in the dictionary's keys,
for key in dict.keys():
    # add the key to dictionaryKeys
    dictionaryKeys.append(key)

# View the dictionaryKeys list
dictionaryKeys
['year', 'county', 'fireReports']

Create a list from the dictionary values

# Create a list to place the dictionary values in
dictionaryValues = []

# For each key in the dictionary's Values,
for x in dict.values():
    # add the key to dictionaryValues
    dictionaryValues.append(x)

# View the dictionaryValues list
dictionaryValues
[[2012, 2012, 2013, 2014, 2014],
 ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'],
 [4, 24, 31, 2, 3]]