Junior hpc - 8 months ago 36

Python Question

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,

`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.

Answer

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
```