PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

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

 October 28, 2022     arrays, is-empty, key-value, object, python     No comments   

Issue

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.



Answered By - Daweo
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing