To illustrate my dilemma I'll use the following code.
formatted_list = []
nested_list = [
[
['Earth', 'Northern Hemisphere', 'North America', 'The United States of America'],
['California', 'Kentucky', 'Colorado', 'Oregon'],
['Sacramento', 'Frankfurt', 'Denver', 'Salem']
],
[
['Earth', 'Northern Hemisphere', 'North America', 'The United States of America'],
['Florida', 'Kentucky', 'Nevada', 'Oregon'],
['Tallahassee', 'Frankfurt', 'Carson City', 'Salem']
]
]
for values in nested_list:
for global_attributes in values[0]:
formatted_list.append(global_attributes)
for values in nested_list:
formatted_list.append(dict(zip(values[1], values[2])))
print(formatted_list)
Now lets say I'm an alien scout and I'm trying to write a python program that will tell my mothership the location of state capitals using a nested list. ['Earth', 'Northern Hemisphere', 'North America', 'The United States of America']
obviously applies to all states and their capitals. However not every state has the same capital. My current code gives the following:
['Earth', 'Northern Hemisphere', 'North America', 'The United States of America', 'Earth', 'Northern Hemisphere', 'North America', 'The United States of America', {'California': 'Sacramento', 'Kentucky': 'Frankfurt', 'Colorado': 'Denver', 'Oregon': 'Salem'}, {'Florida': 'Tallahassee', 'Kentucky': 'Frankfurt', 'Nevada': 'Carson City', 'Oregon': 'Salem'}]
I've created a dictionary that pairs states with their respective cities withing formatted_list
. My question is:
How can I tell python to associate the
['Earth', 'Northern Hemisphere', 'North America', 'The United States of America']
entry with the entire dictionary that follows it?
As in is it possible to have an entire list as a key or value within a dictionary? Thanks for your help.