0

I have a datastructure like this:

d={0:[{'key':'apple', 'count':50},{'key':'ibm', 'count':25}], 1: [{'key':'apple', 'count':40},{'key':'ibm', 'count':29}], 2:[{'key':'apple', 'count':44},{'key':'ibm', 'count':21}]} 

What I want to accomplish is get a list of the values for each of the tuples in the list. What I am trying to do is:

zipped={} count=0 for item in d: #print(item) for i in d[item]: zipped[count]=i['count'] count+=1 

The result I get is:

{0: 50, 1: 25, 2: 40, 3: 29, 4: 44, 5: 21} 

What I want is something like:

{0:[50,25], 1:[40,29], 2:[44,21]} 

How can I get that

1
  • 5
    {k: [mp['count'] for mp in v] for k, v in d.items()} Commented Sep 12, 2022 at 11:20

2 Answers 2

1

res = { key:[value["count"] for value in values] for key,values in d.items()}

use this for your required result

Sign up to request clarification or add additional context in comments.

Comments

0

Yeah, you go too deep. I think you can get away with multiple list comprehension. It should work even if you have an undetermined number of internal values:

zipped = {key: [elem['count'] for elem in internal_list] for key, internal_list in d.items()} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.