mercredi 19 octobre 2016

get dictionary contains in list if key and value exists

How to get complete dictionary data inside lists. but first I need to check them if key and value is exists and paired.

test = [{'a': 'hello'  , 'b': 'world', 'c': 1},
        {'a': 'crawler', 'b': 'space', 'c': 5},
        {'a': 'jhon'   , 'b': 'doe'  , 'c': 8}]

when I try to make it conditional like this

if any((d['c'] is 8) for d in test):

the value is True or False, But I want the result be an dictionary like

{'a': 'jhon', 'b': 'doe', 'c': 8}

same as if I do

if any((d['a'] is 'crawler') for d in test):

the results is:

{'a': 'crawler', 'b': 'space', 'c': 5}

Thanks before.

Aucun commentaire:

Enregistrer un commentaire