The most Pythonic way to check if two ordered lists `l1`

and `l2`

are identical, is to use the `l1 == l2`

operator for element-wise comparison. If all elements are equal and the length of the lists are the same, the return value is `True`

.

**Problem**: Given are two lists `l1`

and `l2`

. You want to perform *Boolean Comparison*: Compare the lists element-wise and return `True`

if your comparison metric returns `True`

for all pairs of elements, and otherwise `False`

.

**Examples**:

l1 = [1, 2, 3, 4, 5] l2 = [1, 2, 3] # compare(l1, l2) --> False l1 = [1, 2, 3, 4, 5] l2 = [1, 2, 3, 5, 4] # compare(l1, l2) --> False l1 = [1, 2, 3, 4, 5] l2 = [1, 2, 3, 4, 5] # compare(l1, l2) --> True

Let’s discuss the most Pythonic ways of solving this problem. Here’s a quick interactive code overview:

**Exercise**: Glance over all methods and run the code. What questions come to mind? Do you understand each method?

Read on to learn about each method in detail!

Table of Contents

## Method 1: Simple Comparison

Not always is the simplest method the best one. But for this particular problem, it is! The equality operator `==`

compares a list element-wise—many Python coders don’t know this!

# 1. Simple Comparison def method_1(l1, l2): return l1 == l2 l1 = [1, 2, 3, 4, 5] l2 = [1, 2, 3] print(method_1(l1, l2)) # False

So, if you just want to learn about the most Pythonic way to solve this problem, look no further.

But if you want to dive into the wonderful world of Python, learning about different interesting and powerful Python functions, read on!

## Method 2: Simple For Loop

The following method is what you’d see from a coder coming from another programming language or from a beginner who doesn’t know about the equality operator on lists (see **Method 1**).

# 2. Simple For Loop def method_2(l1, l2): for i in range(min(len(l1), len(l2))): if l1[i] != l2[i]: return False return len(l1) == len(l2) l1 = [1, 2, 3, 4, 5] l2 = [1, 2, 3] print(method_2(l1, l2)) # False

In the code, you iterate over all indices from 0 to the last position of the smallest list as determined by the part `min(len(l1), len(l2))`

. You then check if both elements at the same position are different. If they are different, i.e., `l1[i] != l2[i]`

, you can immediately return `False`

because the lists are also different.

If you went through the whole loop without returning `False`

, the list elements are similar. But one list may still be longer! So, by returning `len(l1) == len(l2)`

, you ensure to only return `True`

if (1) all elements are equal and (2) the lists have the same length.

A lot of code to accomplish such a simple thing! Let’s see how a better coder would leverage the `zip()`

function to reduce the complexity of the code.

## Method 3: zip() + For Loop

*The zip function takes a number of iterables and aggregates them to a single one by combining the i-th values of each iterable into a tuple for every i.*

Let’s see how you can use the function to make the previous code more concise:

# 3. Zip + For Loop def method_3(l1, l2): for x, y in zip(l1, l2): if x != y: return False return len(l1) == len(l2) l1 = [1, 2, 3, 4, 5] l2 = [1, 2, 3] print(method_3(l1, l2)) # False

Instead of iterating over indices, you now iterate over pairs of elements (the ones zipped together). If the lists have different sizes, the remaining elements from the longer list will be skipped. This way, element-wise comparison becomes simpler and no elaborate indexing schemes are required. Avoiding indices by means of the `zip()`

function is a more Pythonic way for sure!

## Method 4: sum() + zip() + len()

But true Python coders will often avoid a for loop and use a generator expression instead.

- You first create an iterable of Boolean values using the generator expression
`x == y for x, y in zip(l1, l2)`

. - Then, you sum up over the Boolean values (another trick of pro coders) to find the number of elements that are the same and store it in variable
`num_equal`

. - Finally, you compare this with the length of both lists. If all three values are the same, both lists have the same elements and their length is the same, too. They are equal!

# 4. Sum + Zip + Len def method_4(l1, l2): num_equal = sum(x == y for x, y in zip(l1, l2)) return num_equal == len(l1) == len(l2) l1 = [1, 2, 3, 4, 5] l2 = [1, 2, 3] print(method_4(l1, l2)) # False print(method_4([1, 2], [1, 2])) # True

From the methods except the first one using the `==`

operator, this is the most Pythonic way due to the use of efficient Python helper functions like `zip(), len(), and sum()`

and generator expressions to make the code more concise and more readable.

You could also write this in a single line of code!

sum(x == y for x, y in zip(l1, l2)) == len(l1) == len(l2)

If you love Python one-liners, check out my new book** Python One-Liners** with internationally renowned publisher NoStarch press. (Amazon Link)

## Method 5: map() + reduce() + len()

The last method is just to train your functional programming skills.

# 5. map() + reduce() + len() from functools import reduce def method_5(l1, l2): equal = map(lambda x, y: x == y, l1, l2) result = reduce(lambda x, y: x and y, equal) return result and len(l1) == len(l2) l1 = [1, 2, 3, 4, 5] l2 = [1, 2, 3] print(method_5(l1, l2)) # False print(method_5([1, 2, 3], [1, 2, 3])) # True

The `map()`

function combines all pairs of elements to Boolean values (are the two elements equal?). The` reduce()`

function combines all Boolean values performing an `and`

operation. Sure, you can also use the more concise variant using the `all()`

function:

## Method 6: map() + all()

This is the same as the previous method—but using the `all()`

function instead of `reduce()`

to combine all Boolean values in a global ** and **operation.

# 6. map() + all() def method_6(l1, l2): result = all(map(lambda x, y: x == y, l1, l2)) return result and len(l1) == len(l2) l1 = [1, 2, 3, 4, 5] l2 = [1, 2, 3] print(method_5(l1, l2)) # False print(method_5([1, 2, 3], [1, 2, 3])) # True

Thanks for reading this article to the end! I hope you learned something new today. If you want to learn something new every day, join my free Python email series for continuous improvement in Python and computer science.

## Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation. To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

**You build high-value coding skills by working on practical coding projects!**

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

If your answer is ** YES!**, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.