Home > Software engineering >  Loop through nested dictionary
Loop through nested dictionary

Time:11-30

I have nested dictionary which look like this:

{
'location': {0: 'London', 1: 'London', 2: 'London', 3: 'London', 4: 'London', 5: 'London', 6: 'London'}, 
'attraction': {0: 'museum', 1: 'museum', 2: 'museum', 3: 'museum', 4: 'museum', 5: 'museum', 6: 'museum'},
'name': {0: 'London Museum', 1: 'British Museum', 2: 'Natural History Museum', 3: 'London's Docklands Museum', 4: 'Science Museum  ', 5: 'London Transport Museum ', 6: 'Victoria and Albert Museum'},
'rate': {0: 4.6, 1: 4.7, 2: 4.7, 3: 4.6, 4: 4.5, 5: 4.5, 6: 4.7}, 
'totalRates': {0: 13873, 1: 115863, 2: 13497, 3: 4528, 4: 51675, 5: 7349, 6: 42541},
'tag': {0: 'Muzeum', 1: 'Muzeum', 2: 'Muzeum', 3: 'Muzeum', 4: 'Muzeum', 5: 'Muzeum', 6: 'Muzeum'},
'address': {0: '150 London Wall', 1: 'Great Russell St', 2: 'Cromwell Rd', 3: '1 Warehouse. West India Quay. No. Hertsmere Rd', 4: 'Exhibition Rd', 5: 'Stare autobusy. pociągi i tramwaje Londynu', 6: 'Cromwell Rd'},
'description': {0: 'Some description', 1: 'Some description', 2: 'Some description', 3: 'Some description', 4: 'Some description', 5: 'Some description', 6: 'Some description'},
'image': {0: 'https://someImage.com/something', 1: 'https://someImage.com/something', 2: 'https://someImage.com/something', 3: 'https://someImage.com/something', 4: 'https://someImage.com/something', 5: 'https://someImage.com/something', 6: 'https://someImage.com/something'}
}

I would like to loop through and get information about every element which would look like this:

{
'location': 'London',
'attraction': 'museum',
'name': 'London Museum',
'rate': 4.6,
'totalRates': 13873,
'tag': 'Muzeum',
'address': '150 London Wall',
'description': 'Some description',
'image': 'https://someImage.com/something'
},
{
'location': 'London',
'attraction': 'museum',
'name': 'British Museum',
'rate': 4.7,
'totalRates': 115863,
'tag': 'Muzeum',
'address': 'Great Russell St',
'description': 'Some description',
'image': 'https://someImage.com/something'
},
ect...

The way i obtain this nested dictionary is by converting pandas.DataFrame with .to_dict() method.

Is it possible to get the result like above with looping through or converting dataframe any different way? I need the result as dictionary in order to show data via django context.

CodePudding user response:

You need to specify orient=records in to_dict():

>>> df.to_dict("records")

[{'location': 'London',
  'attraction': 'museum',
  'name': 'London Museum',
  'rate': 4.6,
  'totalRates': 13873,
  'tag': 'Muzeum',
  'address': '150 London Wall',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'British Museum',
  'rate': 4.7,
  'totalRates': 115863,
  'tag': 'Muzeum',
  'address': 'Great Russell St',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'Natural History Museum',
  'rate': 4.7,
  'totalRates': 13497,
  'tag': 'Muzeum',
  'address': 'Cromwell Rd',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': "London's Docklands Museum",
  'rate': 4.6,
  'totalRates': 4528,
  'tag': 'Muzeum',
  'address': '1 Warehouse. West India Quay. No. Hertsmere Rd',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'Science Museum  ',
  'rate': 4.5,
  'totalRates': 51675,
  'tag': 'Muzeum',
  'address': 'Exhibition Rd',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'London Transport Museum ',
  'rate': 4.5,
  'totalRates': 7349,
  'tag': 'Muzeum',
  'address': 'Stare autobusy. pociągi i tramwaje Londynu',
  'description': 'Some description',
  'image': 'https://someImage.com/something'},
 {'location': 'London',
  'attraction': 'museum',
  'name': 'Victoria and Albert Museum',
  'rate': 4.7,
  'totalRates': 42541,
  'tag': 'Muzeum',
  'address': 'Cromwell Rd',
  'description': 'Some description',
  'image': 'https://someImage.com/something'}]

CodePudding user response:

Assuming you have the data in a variable called "dictionary", something like this should work.

element_dicts = []
for i in range(len(dictionary['location'].keys())):
    data = dict()
    for each in range(len(dictionary.keys())):
         data[each] = dictionary[each][i]
    element_dicts.append(data)

"element_dicts" will now be a list with the dictionary for each place at the ith index.

CodePudding user response:

nm = list(d.keys())
n = len(d[nm[0]])
[{k: d[k][i] for k in nm} for i in range(n)]
  • Related