Python Forum
convert List of Dicts into a 2 deep Nested Dict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert List of Dicts into a 2 deep Nested Dict
#1
Hi

Ive been banging my head against a wall for some time with this now, so hoping somebody will be able to point me in the right direction

I am writing a flask app and my query returns the following list of dictionaries (via cur.fetchall) ..

MyListofDicts = [{'gameID': 'game_1',
  'prediction': 41,
  'bonus': 'no',
  'userName': 'Paul'},
 {'gameID': 'game_2',
  'prediction': 77,
  'bonus': 'no',
  'userName': 'Paul'},
 {'gameID': 'game_1',
  'prediction': 62,
  'bonus': 'no',
  'userName': 'Steve'},
 {'gameID': 'game_2',
  'prediction': 77,
  'bonus': 'yes',
  'userName': 'Steve'}
]
I need to convert this into a nested dictionary so that i can use it to build an html table with jinja2 templating ...The final HTML table will have 'gameID' as Y axis, 'userName' as X axis, with prediction as the value in the table.. there could be any number of games or users in any particular week



Ideally, for me to iterate over it in jinja2, i need to convert the above 'flat' list of dictionaries to a nested dictionary keyed on 'userName' and then 'gameID' ..so that it looks like this


MyDict = {
  'Paul': {
      'game_1': {
                'prediction': 'home_win', 'bonus': 'no'
                  },
      'game_2': {
                'prediction': 'away_win', 'bonus': 'no'
                  }},
   'Steve': {
       'game_1': {
                'prediction': 'home_win', 'bonus': 'no'
                  },
       'game_2': {
                'prediction': 'away_win', 'bonus': 'yes'
                  }
          }
   }
This format will enable me to iterate over on a per user /per game basis in Jinja2 and thus enable me to build a table


Does anyone have any idea on how i can perform the above conversion ...ive been trying for ages now and really struggling to figure it out :(

Any help would be greatly appreciated
Reply
#2
It should be pretty easy. Start with an empty main dictionary. Loop through the list. If the user name is not in the main dictionary, add it as a key with an empty sub-dictionary as the value. Then add the game to the sub-dictionary.

Try that. If it doesn't work, show us the code you wrote and what the problem is, and we can help you fix it.
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 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  convert string to float in list jacklee26 6 1,814 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  convert a list to links Pir8Radio 3 1,043 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  convert this List Comprehensions to loop jacklee26 8 1,418 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,161 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,413 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Convert nested sample json api data into csv in python shantanu97 3 2,725 May-21-2022, 01:30 PM
Last Post: deanhystad
  Convert list to interger Clives 5 1,541 May-09-2022, 12:53 PM
Last Post: deanhystad
  Convert python dataframe to nested json kat417 1 6,244 Mar-18-2022, 09:14 PM
Last Post: kat417
  Updating nested dict list keys tbaror 2 1,243 Feb-09-2022, 09:37 AM
Last Post: tbaror

Forum Jump:

User Panel Messages

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