I'm writing a for-loop that basically iterates over a product of two lists. In most cases, two lists are defined, hence there is no problem with iterating over the product. However, there are also cases where one of the two lists is not defined. Furthermore, there can be a case neither of the lists are defined. Can I change the function bar in the following code snippet, so that it only has a single for-loop that handles various cases without if statements?
import itertools
def foo(a, b):
print(a, b)
def bar(lista, listb):
# How can I make this function more concise?
if lista and listb:
for a, b in itertools.product(lista, listb):
foo(a, b)
elif lista:
for a in lista:
foo(a, listb)
elif listb:
for b in listb:
foo(lista, b)
else:
foo(lista, listb)
print("Case #1. When both lists are defined.")
lista = [1, 2]
listb = [3, 4]
bar(lista, listb)
print("Case #2. When only lista defined.")
lista = [1, 2]
listb = None
bar(lista, listb)
print("Case #3. When only listb is defined.")
lista = None
listb = [3, 4]
bar(lista, listb)
print("Case #4. When neither of two list are defined.")
lista = None
listb = None
bar(lista, listb)