Check if list is the same

7

I have the following list:

x = [([1,2], [1, 2]), ([1,2], [4, 5])]

And I wanted to check if the first list of each tuple is always the same.

Note: List x contains tuples with lists. These tuples may vary, meaning there may be more tuples with other values.

    
asked by anonymous 28.04.2015 / 20:20

1 answer

6

I would use Python's all () method one for the tuples.

Example:

a = [([1, 2], [1, 2]), ([1, 2], [4, 5])] # primeira lista igual
b = [([1, 2], [1, 2]), ([1, 3], [4, 5])] # primeira lista diferente
c = [([1, 2], [2, 2]), ([1, 2], [4, 5]), ([1, 2], [4, 2]), ([1, 2], [1, 6]), ([1, 2], [6, 3])]
d = [([1, 2, 3, 4], [2, 2]), ([1, 2, 3, 4], [4, 5]), ([1, 2, 3, 4], [4, 2]), ([1, 2, 3, 4], [1, 6]), ([1, 2, 3, 4], [6, 3])]

all([y == x[0] for y in [x[0] for x in a]])
True

all([y == x[0] for y in [x[0] for x in b]])
False

all([y == x[0] for y in [x[0] for x in c]])
True

all([y == x[0] for y in [x[0] for x in d]])
True
    
28.04.2015 / 22:07