Online Tutorials & Training Materials | STechies.com
Register Login

How to Compare Two Lists in Python using Set(), Cmp() and difference() Functions

|| 0

How to Compare Two Lists in Python using Set(), Cmp() and difference() Functions
Stechies

Python Difference Between Two Lists

Lists in Python can be performed in different ways but it depends on the outcome required. Two very common methods of comparison are set() and cmp().

The set() function creates an object that is a set object. The cmp() function is used to compare two elements or lists and return a value based on the arguments passed.

In this tutorial you will learn how to compare two lists with the help of set(), cmp(), and difference() functions.

What is Set() Function in Python?

The set() function in Python uses to take an argument and convert it into a set object. It can take arguments like lists, tuples and dictionaries. The argument is called iterable. The output of elements might not be in the same order because items passed as list were not in order.

Code example of set() function

# initializing list    and convert into set object
n = set(['n1','n4','n3','n2'])

#Add new Element in set n
n.add('n5');

print("Output with set Function : ")
print(n)

Output:

Output with set Function :
{'n5', 'n4', 'n1', 'n2', 'n3'}

Code example using Set() function

At first, we convert a list into set by using set() function, now we need to check if both the lists are equal or not by using if operator.

# Python 3 code 
# check if list are equal 
# using set()

# initializing list and convert into set object
x = set(['x1','rr','x3','e4'])
y = set(['x1','rr','e4','x3'])

print ("List first: " + str(x))
print ("List second: " + str(y))

# check if list x equals to y
if x == y:
    print("First and Second list are Equal")
else:
    print("First and Second list are Not Equal")

Output:

List first: {'x3', 'x1', 'rr', 'e4'}
List second: {'x3', 'x1', 'rr', 'e4'}
First and Second list is Equal

Code example using set() & difference() functions

In the following example, we first convert a list into set by using set() function then we need to differentiate between these two sets by using difference() function and use the if() condition to check the return value.

# Python 3 code 
# check if list are equal 
# using set() & difference()

# initializing list and convert into set object
x = set(['x1','rr','x3','y4'])
y = set(['x1','rr','rr','y4'])

print ("List first: " + str(x))
print ("List second: " + str(y))

# take difference of two lists
z = x.difference(y)

print("Difference of first and second String: " + str(z))

# if lists are equal
if not z:
    print("First and Second list are Equal")
# if lists are not equal    
else:
    print("First and Second list are Not Equal")

Output:

List first: {'y4', 'x3', 'rr', 'x1'}
List second: {'y4', 'rr', 'x1'}
Difference of first and second String: {'x3'}
First and Second list are Not Equal

Code example using sort() and == operator

In this example, we first sort the list so that element of the list is in the same order and then compare both the list with == operator

# Python 3 code 
# check if list are equal 
# using sort() & == operator

# initializing list and convert into set object
x = ['x1','rr','x3','y4']
y = ['x1','rr','rr','y4']

print ("List first: " + str(x))
print ("List second: " + str(y))

# sort list x and y
x.sort()
y.sort()

# if lists are equal
if x == y:
    print("First and Second list are Equal")
# if lists are not equal    
else:
    print("First and Second list are Not Equal")

Output:

List first: ['x1', 'rr', 'x3', 'y4']
List second: ['x1', 'rr', 'rr', 'y4']
First and Second list are Not Equal

Custom Function

In this example, we need to check the elements one by one whether it's available in List 1 or List2.

# Custom python code to check if list one is equal to list two by taking difference
# Define function name difference

def difference (list1, list2):
   list_dif = [i for i in list1 + list2 if i not in list1 or i not in list2]
   return list_dif
   
# Initializing list 1 and list 2
x = [10, 15, 20, 25, 30, 35, 40]
y = [25, 40, 35]

print ("List first: " + str(x))
print ("List second: " + str(y))

# Take difference of list 1 and list 2
z = difference (x, y)

print("Difference of first and second String: " + str(z))

# if lists are equal
if not z:
    print("First and Second list are Equal")
# if lsts are not equal    
else:
    print("First and Second list are Not Equal")

Output: 

List first: [10, 15, 20, 25, 30, 35, 40]
List second: [25, 40, 35]
Difference of first and second String: [10, 15, 20, 30]
First and Second list are Not Equal

What is cmp() Function in Python?

The cmp() function is a built-in method in Python used to compare the elements of two lists. The function is also used to compare two elements and return a value based on the arguments passed. This value can be 1, 0 or -1.

Note: cmp() build to function for python 2, In python 3 it is not available.

For example, if a and b are two lists, then

If a>b, then value 1 is returned 
If a<b, value -1 is returned 
If a=b, value 0 is returned 

Below is an example of two lists being compared using the cmp() function. 

#use of cmp() method
#where a=b, a<b and a>b these three comparison.

#when a<b

a = 1
b = 2

print(cmp(a, b))

#when a = b

a = 2
b = 2

print(cmp(a, b))

#when a>b

a = 3
b = 2

print(cmp(a, b))

Output

a<b is true and results is -1.
where a=b are equal it returns 0.
where a>b  is the output is 1.