简体   繁体   中英

List comprehension for dict in dict

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

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()]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM