Compare list of lists Python

    Python » Python Tutorial

How to Compare Two Lists in Python using set[], cmp[] and difference[] Functions

While working with lists in Python, you might have encountered two lists which seem similar. To figure out the difference, you have to compare the data items of both lists. You can do this by using the set[], difference[] and sort[] methods.

In this article, we will understand how to compare two lists in Python.

Comparing lists in Python

There are different ways to compare lists in Python. But it depends on the outcome required. Two of the most popular methods are set[] and cmp[].

Theset[] functioncreates an object that is a set object. Thecmp[] functionis used to compare two elements or lists and return a value based on the arguments passed.

In the following sections, we will see the application 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.

Example of set[] Function

1] Initializing List and Convert into Set Object

# 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'}

2] Checking if List Are Equal Using set[] Function

At first, we convert a list into the set by using a 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 isEqual

Example Using Set[] & Difference[] Functions

In the following example, we first convert a list into the 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

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

Comparing two lists in Python using a 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 version 2, In python version 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

Chủ Đề