Home python multiple if conditions

multiple if conditions

Author

Date

Category

Hello everyone! there is a construction like:

if a in b:
  print (a)
elif a in c:
  print (a)
elif a in d:
  print (a)
else:
  pass

how to make the variable a print only when ALL if-conditions are fulfilled, and if at least one of them is not fulfilled, then pass? only crutches come to mind, but I wanted a laconic solution


Answer 1, authority 100%

If all conditions must be met:

if (a in b and a in c and a in d):
  print (a)
else:
  pass

If at least one condition must be met:

if (a in b or a in c or a in d):
  print (a)
else:
  pass

Answer 2, authority 98%

To check if an element is contained in all available collections:

if all (item in it for it in collections):
  print (item)

where for example: collections = b, c, d .

To check if an element is contained in any from the collections:

if any (item in it for it in collections):
  print (item)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions