How can I use list comprehension to get some data from another list?
问题 Hello I have the following list : a = [{'Hello':5, 'id':[{'cat':'billy', 'dog': 'Paul'}, {'cat':'bill', 'dog': 'Pau'}]}, {'Hello':1, 'id':[{'cat':'Harry', 'dog': 'Peter'}, {'cat':'Hary', 'dog': 'Pete'}]}] and I would like to build the following list (using list comprehensions): b = ['billy', 'bill', 'Hary', 'Harry'] I tried these without success: [x for y in a for b in y['id'] for x in b] [x for y in a for b in y['id'] for x in b['cat']] 回答1: If you want to use a double loop: [x['cat'] for y