Compare Two Lists Python

Lists are the alternative to arrays in Python, which store collections of different types of items. You will learn about comparing two lists in Python.

Also, remember lists are mutable, which means you can manipulate the lists, so here you will learn about different methods to compare two lists in Python.

Compare Two Lists Python

You can compare two lists in Python using different approaches, such as the sort() and sorted() methods with the == operator or the set() function with the == operator. There are other ways exist to compare two lists in Python.

But I will use these three most commonly used methods.

How to Compare Two Lists in Python Using sort() Method

The sort() method of Python sorts the list, and it sorts the original list. Here, when the list is sorted, all the items same items in the list will have the same index position. then, using the == operator, you can compare two lists.

For example, look at the code below for how two lists are compared.

#creating two list required_items and packed_items
required_items = ['apple', 'banana', 'cherry']
packed_items = ['banana', 'apple', 'cherry']

#sorting each list using the sort() method
required_items.sort()
packed_items.sort()

#comparing list to see if they are equal or not
if required_items == packed_items:
  print("Lists are equal")

else:
  print("Lists are not equal")
How to Compare Two Lists in Python Using sort() Method

From the output, you can see that the two list required_items and packed_items are equal which is printed on the terminal as output.

READ:  How to fix SyntaxError invalid character in identifier in Python? [3 Examples]

Python Compare Two Lists using sorted() Method

The sorted() method is similar to the sort() method, but the sorted() method makes a copy of the original list and sorts that copy.

Let’s take the same example that you have used in the above section.

#creating two list required_items and packed_items
required_items = ['apple', 'banana', 'cherry']
packed_items = ['banana', 'apple', 'cherry']

#sorting each list using the sorted() method
list1=sorted(required_items)
list2=sorted(packed_items)

#comparing list to see if they are equal or not
if list1 == list2:
  print("Lists are equal")

else:
  print("Lists are not equal")
Python Compare Two Lists using sorted() Method

From the output, you can see that both lists list1 and list2 are equal. Here, the sorted() method creates a copy of the original list, sorts it, and then stores it in new variables named list1 and list2.

Python Compare Lists Using set() Method

The set() method of Python takes the value and converts it into a new set. So, here, you can use the set() method to convert the list into the set and then compare the list using the == operator.

Let’s use the same example of lists required and packed.

#creating two list required_items and packed_items
required_items = ['apple', 'banana', 'cherry']
packed_items = ['banana', 'apple', 'cherry']

#creating set from the list using the set() method
list1=set(required_items)
list2=set(packed_items)

#comparing list to see if they are equal or not
if list1 == list2:
  print("Lists are equal")

else:
  print("Lists are not equal")
Python Compare Lists Using set() Method

Here, lists required_tiems and packed_items are converted into sets using the set() method without considering the order of the elements in the list. As a result, the list contains the same number of elements, so it is equal.

Conclusion

In this Python tutorial, you learned how to compare two lists in Python using the set(), sort() and sorted() in-built method of Python.

READ:  How to Prepend to a List in Python [6 Methods]

You may like to read: