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

yourstruly picture yourstruly · Aug 30, 2015 · Viewed 26.1k times · Source

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?

Answer

hspandher picture hspandher · Aug 31, 2015

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.