-1

I have the following python dictionary (a dict in a dict):

d = {'k1': {'kk1':'v1','kk2':'v2','kk3':'v3'},'k2':{'kk1':'v4'}}

I can't get my brains to figure out the list comprehension to get a list of all values (v1, v2...). If you can give my an example with a lambda also, that you be nice.

The goal is to have values_lst = ['v1','v2','v3','v4']

Thanks

1 Answers1

0

Combine two loops to "flatten" the dict of dicts. Loop over the values of d first, and then loop over the values of the values of d. The syntax might be a bit hard to grasp at first:

values_lst = [v for x in d.values() for v in x.values()]
Jan Christoph Terasa
  • 5,781
  • 24
  • 34