Search code examples
pythonarraysobjectkey-valueis-empty

How to check if ANY element inside a Python Object is empty


I have an array of objects:

 dataArray = [{thing1: foo, thing2: baa, thing3:foobaa},{thing1: doo, thing2: daa, thing3: doodaa}]

Which I am editing in a function by iterating over the lines:

  for obj in dataArray:
      DO STUFF

I want to skip the whole object if ANY of the values are empty.

e.g. if

  dataArray['thing2'] == ''

Is there a way to generalise this without having to iterate though all the keys in the obj?


Solution

  • You can use all function for this task following way, consider following example

    d = {"A": "abc", "B": "", "C": "ghi"}
    any_empty = not all(d.values())
    print(any_empty)
    

    output:

    True
    

    I used all here to detect if all values are truth-y then negate is at will be False if any value is False. Note that this is actually sensitive to bool(value) so it will also detect None, False and so on, so please consider if this is acceptable in your case.