Reputation: 901
I want to use list comprehensions to covert the above into a list of tuples but with only a subset of each dictionary in the tuple.
SOURCE
source = [
{'gem': 'gold', 'dimensions': {"weight":120, "height":0, "color":240}, 'shine': '90', 'worth': 10000},
{'gem': 'diamond', 'dimensions': {"weight":80, "height":20, "color":10}, 'shine': '190', 'worth': 5000}
...
]
From the source above, the list should look like this once completed:
[(120, 240),(80, 10) ]
Each tuple in the list is based on the the dict at the matching index in Source but only parts of a nested dict are selected for inclusion in the type:
Desired Dataset
[(dimensions.weight, dimensions.color), ...]
I've tried several different things but cannot get further than this:
[g["dimensions"] for g in source]
which yields
[{"weight":120, "height":0, "color":240}, {"weight":80, "height":20, "color":10}]
Upvotes: 0
Views: 53
Reputation: 1047
You're so close, just need to specify the values you want to include in the tuple
[(g["dimensions"]["weight"], g["dimensions"]["color"]) for g in source]
Upvotes: 3