user765443 user765443 - 1 year ago 109
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)

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download