How to compare two lists in python

In this article, you will learn about how to compare two lists in python. In the Python programming language, a list is considered as an…


This content originally appeared on CodeSource.io and was authored by Md Niaz Rahman Khan

In this article, you will learn about how to compare two lists in python.

In the Python programming language, a list is considered as an ordered container that is mutable. You can store similar types of items in a list. In python, sometimes you need to work with more than one list and compare them. To compare two lists in python, there are some useful methods like – list.sort() and equal operator, collections.Counter(), sum(), zip() and, len(). In this article, we will explore these approaches one by one for comparing two lists in Python.

list.sort() and == operator

In this approach, lists are compared in two steps. First using the sort() method the lists are sorted and then using the double equal operator compares the list item and check if they are equal or not. Let’s see an example of it to understand things more clearly.

list1 = [10, 20, 50, 30, 40]
list2 = [10, 20, 30, 50, 40]

# sort the lists using sort()
list1.sort()
list2.sort()

if list1 == list2:
	print ("Lists are Equal")
else :
	print ("Lists are not Equal")

# Output: Lists are Equal

Here, you can see that we have taken two lists and the list items are the same but remain unsorted. So we have sorted them first using the sort() method and then compare them with the double equal operator and in our case, it gives output as lists are equal. In your case, if the items are not the same for both lists then it will return that lists are not equal.

collections.Counter()

In the previous approach, we need to sort both list items first. But using collections.Counter(() method we don’t need to sort them in a certain order. It checks if the elements of the two lists are the same or not. See the below code example:

import collections
   
list1 = [10, 20, 50, 30, 40]
list2 = [40, 20, 10, 50, 30]
  
# collections.Counter() is used
if collections.Counter(list1) == collections.Counter(list2):
    print ("Lists are Equal")
else :
    print ("Lists are not Equal")

# Output: Lists are Equal

Here, we first import collections to use collections.Counter() method and you can see that the list items are the same for both lists but they are not in order.

sum() ,zip() and len()

In this approach, the summation of both list items has been done first and then compare their length. The list items are needed to be sorted for this approach. If the list items are the same but are not sorted then it will consider it as unequal. See the below code example

list1 = [10, 20, 30, 40, 50]
list2 = [10, 20, 30, 40, 50]

# sum(), zip(), len() are used
if len(list1)== len(list2) and len(list1) == sum([1 for i, j in zip(list1,list2) if i == j]):
	print ("Lists are Equal")
else :
	print ("Lists are not Equal")

# Output: Lists are Equal

Here, you can see that both list items are in the same order and as a result, we are getting the output as the lists are equal.

These are some useful approaches that you may follow for comparing two lists in the Python programming language. In this whole article, we have shown the output as lists are equal. Your task is to try out different list items and see if they are equal or not.


This content originally appeared on CodeSource.io and was authored by Md Niaz Rahman Khan


Print Share Comment Cite Upload Translate Updates
APA

Md Niaz Rahman Khan | Sciencx (2022-02-01T15:52:11+00:00) How to compare two lists in python. Retrieved from https://www.scien.cx/2022/02/01/how-to-compare-two-lists-in-python/

MLA
" » How to compare two lists in python." Md Niaz Rahman Khan | Sciencx - Tuesday February 1, 2022, https://www.scien.cx/2022/02/01/how-to-compare-two-lists-in-python/
HARVARD
Md Niaz Rahman Khan | Sciencx Tuesday February 1, 2022 » How to compare two lists in python., viewed ,<https://www.scien.cx/2022/02/01/how-to-compare-two-lists-in-python/>
VANCOUVER
Md Niaz Rahman Khan | Sciencx - » How to compare two lists in python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/01/how-to-compare-two-lists-in-python/
CHICAGO
" » How to compare two lists in python." Md Niaz Rahman Khan | Sciencx - Accessed . https://www.scien.cx/2022/02/01/how-to-compare-two-lists-in-python/
IEEE
" » How to compare two lists in python." Md Niaz Rahman Khan | Sciencx [Online]. Available: https://www.scien.cx/2022/02/01/how-to-compare-two-lists-in-python/. [Accessed: ]
rf:citation
» How to compare two lists in python | Md Niaz Rahman Khan | Sciencx | https://www.scien.cx/2022/02/01/how-to-compare-two-lists-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.