-2

I have two variables, a and b, and I have some combinations:

  • a is blank and b is None
  • a is None and b is blank
  • a is blank and b is blank
  • a is None and b is None

and I need to match all the four conditions in one line. I am trying to do with:

if ((a, b) in ['', None, '', None]))

However, this does not produce the desired result.

Georgy
  • 12,464
  • 7
  • 65
  • 73
AGaur
  • 185
  • 1
  • 1
  • 15
  • 1
    You're asking if a tuple is in a list of strings and Nones, which it never will be. Ask instead if it's in a list *of tuples*, where it might be. – jonrsharpe Oct 22 '19 at 07:18
  • Possible duplicate of [Can Python test the membership of multiple values in a list?](https://stackoverflow.com/questions/6159313/can-python-test-the-membership-of-multiple-values-in-a-list) – Georgy Oct 22 '19 at 08:26
  • "all the four conditions in one line" - why? Do you have a shortage of newline characters in your environment? – paxdiablo Aug 10 '20 at 07:53

3 Answers3

1

Just use a list of tuples:

if (a,b) in [(None, None),("", None),("", ""),(None, "")]:
Florian H
  • 3,052
  • 2
  • 14
  • 25
1

you could use a set:

if set((a, b)) <= set((None, "")):

<= between sets tests if one is a subset of the other.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
-1

do this

cond = [('', None),(None,''),('',''),(None, None)] 
if (a,b) in cond:
    # code 
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • Although this code might solve the problem, a good answer should also explain **what** the code does and **how** it solves the problem. – BDL Oct 22 '19 at 08:13
  • @BDL `print('hello world)` no point to explain what print does or how it works, above code is in plain english and easy to understand – sahasrara62 Oct 22 '19 at 08:18