Skip to content Skip to sidebar Skip to footer

One If Statement With Unknown Number Of Conditions Python

I have a list of list that contains all the conditions that the if statement has to satisfy, but the problem is that the number of conditions stored into the list of list is unknow

Solution 1:

You can use a generator expression within all():

if all(i == j for i, j in my_list): # use int(j) if'j'is string and 'i'is integer.
    # do something

Solution 2:

I think @Kasramwd provides the most pythonic solution, but an alternative makes use of Python's else clause on a for loop.

for item in my_list:
    if item[0] != item[1]:
        breakelse:
    # do-something

Post a Comment for "One If Statement With Unknown Number Of Conditions Python"