Python check if value is in a list of dicts

后端 未结 5 1418
陌清茗
陌清茗 2021-02-08 23:56

I have a list of dicts e.g.

[{\'name\':\'Bernard\',\'age\':7},{\'name\':\'George\',\'age\':4},{\'name\':\'Reginald\',\'age\':6}]

I\'d like to c

5条回答
  •  无人及你
    2021-02-09 00:31

    I think a list comprehension would do the trick here too.

    names = [i['name'] for i in l]
    

    Then use it like so:

    'Bernard' in names (True)
    'Farkle' in names (False)
    

    Or a one liner (If it's only one check)

    'Bernard' in [i['name'] for i in l] (True)
    

提交回复
热议问题