 # Python Compare Two Lists

Comparing 2 lists may mean many different things. For example:

• Check if all elements of a list are present in same order in another list.
• Check if all elements of a list are present in another list in any order.
• Getting the intersection element of 2 lists.
• Getting the common elements of 2 lists.
• In case of string element compare ignoring case. We will learn all possible cases of list comparison in Python. We will also learn different methods for the same purpose.

## Compare if 2 lists are equal with same order

To compare something in Python, we use `==` operator or `is` operator.

1. ==(equal to) - It compares 2 values and returns `True` if both values are equal.
2. is(identical to) - It compares 2 variables and returns `True` if both variables are pointing to the same object. This means the memory addresses of both variables are the same.

We are going to use == because when using is we are comparing the memory address of 2 variables which will be different in the case of 2 different lists.

Comparing 2 lists for same order of items

### Method 1: == Operator

We can use the == operator to compare 2 lists. If both lists have the same element in the same order then it will return `True`.

``````# python compare two lists
# Method 1: == Operator
list1 = [1, 2, 3, 4, 5]
list2 = [1, 2, 3, 4, 5]
print(list1 == list2) # True``````

Output:

`True`

### Method 2: Using loop

We can also use for loop to compare 2 lists.

Since we have to check only if the element at any position in list1 is equal to the element at the same position in list2, so we need not use any nested loop. We can compare it in a single iteration.

``````# Method 2: For loop
list1 = [1, 2, 3, 4, 5]
list2 = [1, 2, 3, 4, 5]

for i in range(len(list1)):
if list1[i] != list2[i]:
print(False)
break
else:
print(True)``````

Output:

`True`

## Compare if 2 lists are equal regardless of order

When you compare 2 lists regardless of the order of the elements in the list, it becomes a little complex task. Now you can't just equate 2 lists and check if they are equal.

To compare the lists now you have 2 approaches:

1. Sort both the lists and then compare them.
2. Compare each element of the list1 with each element of the list2 and check if they exist in both lists.

Comparing 2 lists for a different order of items

### Method 1: Sort both the lists

Sort both the lists. This will make the list to be in the same order if they have the same elements.

Now simply compare both the lists using the == operator. ``````# Method 1
list1 = [4, 1, 5, 2, 3]
list2 = [3, 5, 2, 1, 4]

# Sort both the lists
list1.sort()
list2.sort()

# Compare both the lists
print(list1 == list2)``````

Output:

`True`

### Method 2: Compare Individual Element

Another approach is to compare each element of the list1 with each element of list2 and check if they exist in both lists.

Before comparing elements first check if both the lists have the same length. If they don't have the same length then they are simply not equal.

We are going to create a function here that will take 2 lists as arguments and do the comparison.

``````# Python compare two lists individual element
# Method 2
list1 = [4, 1, 5, 2, 3]
list2 = [3, 5, 2, 1, 4]

def compare(list1, list2):
# Check if both the lists have the same length
if len(list1) != len(list2):
print(len(list1), len(list2))
return False

# Check if all the elements of both the lists are same
count = 0
for i in range(len(list1)):
for j in range(len(list2)):
if list1[i] == list2[j]:
count += 1
break
# check if count is equal to length of list
if count == len(list1):
return True
else:
return False

# call the function
print(compare(list1, list2))``````

Output:

`True`

## Get intersection of 2 lists

Finding the intersection of 2 lists means finding all the elements that are present in both lists.

There are many ways to find the intersection of 2 lists. The most common way to get intersections is using a set data structure.

### Method 1: Find intersection using set

A set consists of unique elements. So if take a set of a list then it will return unique elements of the list.

To find the intersection of the lists you can take a set of both the lists and use & operator to find the intersection.

``````# set() + & operator
a = [6, 2, 7, 1, 5]
b = [9, 4, 6, 8, 2]

# find intersection
result = set(a) & set(b)
print(list(result))

result = set(b) & set(a)
print(list(result))``````

Output:

```[2, 6]
[2, 6]```

Both `set(a) & set(b)` or `set(b) & set(a)` will return same result.

You can also use the intersection() method of the set class to find intersections.

``````# set() + intersection()
a = [6, 2, 7, 1, 5]
b = [9, 4, 6, 8, 2]

# find intersection
result = set(a).intersection(set(b))
print(list(result))

result = set(b).intersection(set(a))
print(list(result))``````

Output:

```[2, 6]
[2, 6]```

### Method 2: Find intersection using list comprehension

Another way to find the intersection of 2 lists is using list comprehension.

In list comprehension, we will use the if condition to check if an element exists in both lists.

``````# list comprehension
a = [6, 2, 7, 1, 5]
b = [9, 4, 6, 8, 2]

# find intersection
result = [x for x in a if x in b]
print(result)``````

Output:

`[2, 6]`

Alternatively, you can use for loop to find intersections.

The loop will also follow the same logic as a list comprehension.

``````# for loop
a = [6, 2, 7, 1, 5]
b = [9, 4, 6, 8, 2]

# find intersection
result = []
for x in a:
if x in b:
result.append(x)
print(result)``````

Output:

`[2, 6]`

## Get the difference of 2 lists

Finding the difference of elements from the 1st list to the 2nd list means finding all the elements that are present in the 1st list but not in the 2nd list.

To get the difference between 2 lists you can use a set data structure and use the - operator to find the difference.

``````# set() + - operator
a = [6, 2, 7, 1, 5]
b = [9, 4, 6, 8, 2]

# find difference (a - b)
result = set(a) - set(b)
print(list(result))

# find difference (b - a)
result = set(b) - set(a)
print(list(result))``````

Output:

```[1, 5, 7]
[8, 9, 4]```

The difference between list1 to list2 and list2 to list1 will be different.

You can also use the difference() method of the set class to find differences.

``````# set() + difference()
a = [6, 2, 7, 1, 5]
b = [9, 4, 6, 8, 2]

# find difference (a - b)
result = set(a).difference(set(b))
print(list(result))

# find difference (b - a)
result = set(b).difference(set(a))
print(list(result))``````

Output:

```[1, 5, 7]
[8, 9, 4]```

## Compare if 2 lists are equal ignoring case

Suppose your list contans string values and you want to compare if 2 lists are equal ignoring case.

To do this you can use the lower() method of the str class to convert all the string values to lower case and then use the == operator to compare (If elements are not in the same order then you can use the methods discussed above).

``````# compare two lists ignoring the case

a = ['a', 'B', 'c']
b = ['A', 'b', 'C']

# lower case both lists
a = [x.lower() for x in a]
b = [x.lower() for x in b]

# compare both lists
print(a == b)``````

Output:

`True`

## Conclusion

In this article, we have discussed multiple ways how Python compare two lists. We have seen cases like comparing 2 lists, finding intersections, finding differences, and comparing 2 lists ignoring the case.

Check out: Find max and min in a list in python