Python check if value is in a list of dicts

后端 未结 5 1419
陌清茗
陌清茗 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:49

    smf = [{'name':'Bernard','age':7},{'name':'George','age':4},{'name':'Reginald','age':6}]
    def names(d):
        for i in d:
            for key, value in i.iteritems():
                 if key == 'name':
                     yield value
    
    
    In [5]: 'Bernard' in names(smf)
    Out[5]: True
    
    
    In [6]: 'Bernardadf' in names(smf)
    Out[6]: False
    

提交回复
热议问题