Python Forum
Nested Dictionaries with Lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested Dictionaries with Lists
#1
I have seen similar threads related to creating nested dictionaries, but I'm still having trouble with this particular code. My goal is to use nested dictionaries to sort locations with seasons with values. Keep in mind that I created dummy lists to represent what I'm working with. I am not able to use matrices as I am going to have to remove random bits of information (not set them equal to zero, but actually remove them). I am also applying this code to literally hundreds of thousands of points, so hard-coding is not an option. Here is the code I have come up with so far:

Seasons = ['Spring','Summer','Fall','Winter']
Locations = ['Paris','London','San Francisco']
Number = [[10, 34, 56, 7], [9000, 40, 21, 17], [43, 628, 906, 1]]

FinalDict = dict()
TempDict = dict()

for j in Seasons:
    for i in Locations:
        m = Seasons.index(j)
        n = Locations.index(i)
        TempDict[j] = Number[n][m]
        FinalDict[i] = TempDict

print FinalDict
This is what my dictionary looks like now:

{'Paris': {'Spring': 43, 'Fall': 906, 'Winter': 1, 'Summer': 628}, 'San Francisco': {'Spring': 43, 'Fall': 906, 'Winter': 1, 'Summer': 628}, 'London': {'Spring': 43, 'Fall': 906, 'Winter': 1, 'Summer': 628}}
My goal is to have my dictionary look something like this:

{'Paris': {'Spring': 10, 'Fall': 56, 'Winter': 7, 'Summer': 34}, 'San Fransisco': {'Spring': 43, 'Fall': 906, 'Winter': 1, 'Summer': 628}, 'London': {'Spring': 9000, 'Fall': 21, 'Winter': 17, 'Summer': 40}}
Any help would be greatly appreciated Blush

Thanks!

I'm working with Python 2.7. Forgot to mention.
Reply
#2
You need to create a new TempDict for each location. You only have one TempDict now, so all your sub-dictionaries look the same because they are the same.

Edit: I would also redo your for loops using zip to avoid the backtracking to indexes:

for location, location_data in zip(Locations, Number):
    TempDict = {}
    for season, number in zip(Seasons, location_data):
        TempDict[season] = number
    FinalDict[location] = TempDict
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 912 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,360 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Comparing items from 2 lists of dictionaries illwill 7 2,738 Sep-14-2020, 10:46 PM
Last Post: bowlofred
  Searching through Nested Dictionaries and Lists Dave_London 1 7,302 Jul-09-2020, 03:36 PM
Last Post: mrdominikku
  Creating Nested Dictionaries Confusion gw1500se 2 2,121 May-18-2020, 11:16 PM
Last Post: gw1500se
  Unpacking nested lists yonatan776 1 2,191 Apr-14-2020, 08:50 PM
Last Post: buran
  Help: for loop with dictionary and nested lists mart79 1 1,859 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Finding value in nested dictionaries with lists mart79 16 7,986 Mar-08-2020, 08:16 PM
Last Post: ndc85430
  nested dictionaries to CSV mart79 9 12,424 Jul-29-2019, 04:59 AM
Last Post: mart79
  Transform simplified dictionary to nested dictionaries bhojendra 1 2,360 Jul-02-2019, 02:05 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020