Home > front end >  Iterate through multiple list of dictionaries
Iterate through multiple list of dictionaries

Time:09-13

I would like to iterate through list of dictionaries in order to get a specific value, but I can't figure it out.

I've made a simplified version of what I've been working with. These lists or much longer, with more dictionaries in them, but for the sake of an example, I hope this shortened dataset will be enough.

listOfResults = [{"29":2523,"30":626,"10":0,"32":128},{"29":2466,"30":914,"10":0,"32":69}]

For example, I need the values of the key "30" from the dictionaries above. I've managed to get those and stored them in a list of integers. ( [626, 914] )

These integers are basically IDs. After this, I need to get the value of these IDs from another list of dictionaries.

listOfTrack = [{"track_length": 1.26,"track_id": 626,"track_name": "Rainbow Road"},{"track_length": 6.21,"track_id": 914,"track_name": "Excalibur"}]

I would like to print/store the track_names and track_lengths of the IDs I've got from the listOfResults earlier. Unfortunately, I've ended up in a complete mess of for loops.

CodePudding user response:

You want something like this:

ids = [626, 914]
result = { track for track in list_of_tracks if track.get("track_id") in ids }

CodePudding user response:

I unfortunately can't comment on the answer given by Nathaniel Ford because I'm a new user so I just thought I'd share it here as an answer.

His answer is basically correct, but I believe you need to replace the curly braces with brackets or else you will get this error: TypeError: unhashable type: 'dict'

The answer should look like:

ids = [626, 914]
result = [track for track in listOfTrack if track.get("track_id") in ids]

CodePudding user response:

listOfResults = [{"29":2523,"30":626,"10":0,"32":128},{"29":2466,"30":914,"10":0,"32":69}]

ids = [x.get('30') for x in listOfResults]

listOfTrack = [{"track_length": 1.26,"track_id": 626,"track_name": "Rainbow Road"},{"track_length": 6.21,"track_id": 914,"track_name": "Excalibur"}]

out = [x for x in listOfTrack if x.get('track_id') in ids]

Alternatively, it may be time to learn a new library if you're going to be doing a lot this.

import pandas as pd

results_df = pd.DataFrame(listOfResults)
track_df = pd.DataFrame(listOfTrack)

These Look like:

# results_df
     29   30  10   32
0  2523  626   0  128
1  2466  914   0   69

# track_df
   track_length  track_id    track_name
0          1.26       626  Rainbow Road
1          6.21       914     Excalibur

Now we can answer your question:

# Creates a mask of rows where this is True.
mask = track_df['track_id'].isin(results_df['30'])
# Specifies that we want just those two columns.
cols = ['track_length', 'track_name']
out = track_df.loc[mask, cols]
print(out)

# Or we can make it back into a dictionary:
print(out.to_dict('records'))

Output:

   track_length    track_name
0          1.26  Rainbow Road
1          6.21     Excalibur

[{'track_length': 1.26, 'track_name': 'Rainbow Road'}, {'track_length': 6.21, 'track_name': 'Excalibur'}]
  • Related