I have a dictionary dict1['a'] = [ [1,2], [3,4] ] and need to generate a list out of it as l1 = [2, 4]. That is, a list out of the second element of each inner list. It can be a separate list or even the dictionary can be modified as dict1['a'] = [2,4].
I have a dictionary dict1[‘a’] = [ [1,2], [3,4] ] and need to generate
Share
Assuming that each value in the dictionary is a list of pairs, then this should do it for you:
As you can see:
dict1.values()gets you just the values in your dict,for pairlist in dict1.values()gets you all the lists of pairs,for pair in pairlistgets you all the pairs in each of those lists,pair[1]gets you the second value in each pair.Try it out. The Python shell is your friend!…
While I’m at it, I’ll just say: ipython loves you!