Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - check if any value of dict is not None (without iterators)

I wonder if it is possible to gain the same output as from this code:

d = {'a':None,'b':'12345','c':None} nones=False for k,v in d.items():    if d[k] is None:     nones=True       

or

any([v==None for v in d.values()]) 

but without a for loop iterator, or generator?

like image 475
yourstruly Avatar asked Aug 30 '15 12:08

yourstruly


People also ask

How do you check if a value in a dictionary is null in Python?

Using bool() The bool method evaluates to true if the dictionary is not empty. Else it evaluates to false. So we use this in expressions to print the result for emptiness of a dictionary.

How do you check if a value exists in a dictionary Python?

Check if a value exists in a dictionary: in operator, values() To check if a value exists in a dictionary, i.e., if a dictionary has/contains a value, use the in operator and the values() method. Use not in to check if a value does not exist in a dictionary.


1 Answers

You can use

nones = not all(d.values()) 

If all values are not None, nones would be set to False, else True. It is just an abstraction though, internally it must iterate over values list.

like image 64
hspandher Avatar answered Oct 05 '22 22:10

hspandher