0

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?

2
  • 1
    On some level -- iteration is required. You could perhaps hide that iteration in a call to any() (a python function whose name appears in the title of your question). Commented Mar 12, 2021 at 11:42
  • if any(v == '' for v in obj.values()): continue? Commented Mar 12, 2021 at 11:43

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.