Getting a list of values from a list of dicts

前端 未结 9 699
执念已碎
执念已碎 2020-11-22 15:22

I have a list of dicts like this:

[{\'value\': \'apple\', \'blah\': 2}, 
 {\'value\': \'banana\', \'blah\': 3} , 
 {\'value\': \'cars\', \'blah\': 4}]
         


        
9条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 15:42

    Follow the example --

    songs = [
    {"title": "happy birthday", "playcount": 4},
    {"title": "AC/DC", "playcount": 2},
    {"title": "Billie Jean", "playcount": 6},
    {"title": "Human Touch", "playcount": 3}
    ]
    
    print("===========================")
    print(f'Songs --> {songs} \n')
    title = list(map(lambda x : x['title'], songs))
    print(f'Print Title --> {title}')
    
    playcount = list(map(lambda x : x['playcount'], songs))
    print(f'Print Playcount --> {playcount}')
    print (f'Print Sorted playcount --> {sorted(playcount)}')
    
    # Aliter -
    print(sorted(list(map(lambda x: x['playcount'],songs))))
    

提交回复
热议问题