user765443 user765443 - 5 months ago 27
Python Question

comparison of list using cmp or ==

I have two list which contain data as I am reading two file and storing into list.

should I use cmp(list1,list2) or (list1 == list2)

#! /usr/bin/env py

data = None
with open("sample",'r+') as f:
data = f.readlines()
data[-1] = "abhishe"
data_1 = None
with open("cp.log",'r+') as f:
data_1 = f.readlines()
data_1[-1] = "Goswami"

print "\n\n\n"
print data == data_1
print cmp(data,data_1)


You will very rarely need to use cmp. cmp has the same effect as testing <, == and >, but it is less readable.

In your case, use == as it will perform deep list equality testing.