Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 unknown. For e.g., the list of list is like this:

my_list: [["A", "0"], ["B", "1"], ["C", "2"]]

so the if should be:

if A==0 and B==1 and C==2:
      #do-something
else:
      pass

since I don't know the number of elements in the list of lists, I cannot do:

if my_list[0][0]==my_list[0][1] and my_list[1][0]==my_list[1][1] and my_list[2][0]==my_list[2][1]:
     #do-something
else:
      pass

how do I solve this problem?

A similar problem has been raised here but there is no a clear explanation/implementation of this problem.

Thanks.

like image 959
Junior hpc Avatar asked Feb 07 '23 10:02

Junior hpc


1 Answers

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
like image 110
Mazdak Avatar answered Feb 13 '23 07:02

Mazdak