Checking that the list of tuples is the same

Is there an easy way to see if a list of tuples in python is the same (same tuple at every position where the tuple is the same if their respective elements are the same)? I know how to manually scroll through the list and compare each item, but was wondering if there are any library functions that could do this?

+3


source to share


2 answers


You can use cmp () to compare items from two lists.

list1 = [('a', 1), ('b', 1)]
list2 = [('a', 1), ('b', 1)]

print cmp(list1, list2)

      



If we have reached the end of one of the lists, the longer list is "bigger". If we exhaust both lists and exchange the same data, the result is a link, meaning 0 is returned.

+4


source


len(list1) == len(list2) and all(a == b for a,b in zip(list1, list2))

      

This was my first guess, but I just tried the obvious and simple solution and it worked too:



list1 == list2

      

+1


source







All Articles