Lets say I have a list of tuples with states and counties:
stList = [('NJ', 'Burlington County'),
('NJ', 'Middlesex County'),
('VA', 'Frederick County'),
('MD', 'Montgomery County'),
('NC', 'Lee County'),
('NC', 'Alamance County')]
For each of these items, I want to zip the state with the county, like this:
new_list = [{'NJ': 'Burlington County'},
{'NJ': 'Middlesex County'},
{'VA': 'Frederick County'},
{'MD': 'Montgomery County'},
{'NC': 'Lee County'},
{'NC': 'Alamance County'}]
I tried something like this, but it doesn't work correctly (it iterates through each 'letter' and zips them individually):
new_list = []
for item in stList:
d1 = dict(zip(item[0], item[1]))
new_list.append(d1)
Returns:
[{'N': 'B', 'J': 'u'},
{'N': 'M', 'J': 'i'},
{'V': 'F', 'A': 'r'},
{'M': 'M', 'D': 'o'},
{'N': 'L', 'C': 'e'},
{'N': 'A', 'C': 'l'}]
To make things more complicated, my end goal is to actually have a list of dictionaries for each state(key), that has the counties(value) as a list. How can I fix the zipped dictionary and then put the counties as a list for each state?
final_list = [{'NJ': ['Burlington County', 'Middlesex County']},
{'VA': 'Frederick County'},
{'MD': 'Montgomery County'},
{'NC': ['Lee County', 'Alamance County'}]