0

I have a list of dictionaries like this :

[{'Name': 'Andri', 'Age': '20'}, {'Name': 'Nova', 'Age':'24'}]

Now my question is how can I verify if Nova is a member of the dictionaries or not?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Does this answer your question? [Check if value already exists within list of dictionaries?](https://stackoverflow.com/questions/3897499/check-if-value-already-exists-within-list-of-dictionaries) OR https://stackoverflow.com/questions/6041981/python-check-if-value-is-in-a-list-of-dicts – Tomerikoo Mar 24 '20 at 07:04

3 Answers3

0

you can do that with filter function.

In [10]: l
Out[10]: [{'Name': 'Andri', 'Age': 20}, {'Name': 'Nova', 'Age': '24'}]

In [11]: r=filter(lambda x: 'Nova' in x.values(),l)

In [12]: list(r)
Out[12]: [{'Name': 'Nova', 'Age': '24'}]
teddcp
  • 1,514
  • 2
  • 11
  • 25
0

how i can verified if Nova is a member of the dictionaries or no

This to me sounds like you just want a True or False if 'Nova' exists.

You can use any() to check if 'Nova' exists in the 'Name' key:

>>> d = [{'Name': 'Andri', 'Age': 20}, {'Name': 'Nova', 'Age': 24}]
>>> any(x['Name'] == 'Nova' for x in d)
True

Or you can check the values() specifically:

>>> any(v == 'Nova' for x in d for v in x.values())
True

Or using the in operator:

>>> any('Nova' in x.values() for x in d)
True
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
-2

if 'Nova' in #dictionaryName.itervalues():

Anon
  • 35
  • 6
  • To name just one problem with this, `itervalues` is only available in [tag:python-2.x] and this question is tagged [tag:python-3.x] – Tomerikoo Mar 24 '20 at 07:09