-3

I have a dictionary in the following format:

d = { 'x' : [1,2], 'y' : [1,3,4], 'z' : [1,1] }

And I am trying to append the values of 'd' to a list in the following format:

lst = [1,2,1,3,4,1,1] 

Hence, 'lst' contains all the values of the dictionary 'd'.

My code :-

 lst = [value for v in d.values()]

But this does not work.

Any suggestions as to how I can put these values in a list?

Thank you.

Luke
  • 79
  • 1
  • 8
  • 1
    `[v for l in d.values() for v in l]`? Your attempt is trying to reference a variable that doesn't exist, so its failure shouldn't surprise you! Note that dictionaries don't guarantee order, so `[1, 2, 1, 3, 4, 1, 1] ` isn't necessarily the output you'll get. – jonrsharpe Nov 26 '15 at 23:48

1 Answers1

1

You can use list comprehension

>>> lst = [item for v in d.values() for item in v]
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48