## Problem Formulation

Given a list in Python. How to check if the list has an even number of elements?

Examples:

`[] --> True`

`[1] --> False`

`[1, 2] --> True`

`[1, 2, 3] --> False`

**Related Article:**

## Method 1: len() and Modulo

The most Pythonic way to check if a list has an even number of elements is to use the modulo expression `len(my_list)%2`

that returns `1`

if the list length is odd and `0`

if the list length is even. So to check if a list has an even number of elements use the expression `len(my_list)%2==0`

.

Here’s a simple code example:

def check_even(my_list): return len(my_list)%2==0 print(check_even([])) # True print(check_even([1])) # False print(check_even([1, 2])) # True print(check_even([1, 2, 3])) # False

As background, feel free to watch the following video on the modulo operator:

The length function is explained in this video and blog article:

A slight variant of this method is the following.

## Method 2: len() and Modulo and bool()

To check if a list has an even number of elements, you can use the modulo expression `len(my_list)%2`

that returns `1`

if the list length is odd and `0`

if the list length is even. So to convert the even value 0 to a boolean, use the built-in `bool()`

function around the result and invert the result, i.e., `not bool(len(my_list)%2)`

.

Here’s a simple code example:

def check_even(my_list): return not bool(len(my_list)%2) print(check_even([])) # True print(check_even([1])) # False print(check_even([1, 2])) # True print(check_even([1, 2, 3])) # False

As background, you may want to look at this explainer video:

## Method 3: Bitwise AND

You can use the expression `len(my_list)&1`

that uses the Bitwise AND operator to return 1 if the list has an even number of elements and 0 otherwise. Now, you simply convert it to a Boolean if needed using the `bool()`

function and invert it using the `not`

operator: `not bool(len(my_list)&1)`

.

Python’s ** bitwise AND** operator

`x & y`

performs *logical AND*on each bit position on the binary representations of integers

`x`

and `y`

. Thus, each output bit is 1 if both input bits at the same position are 1, otherwise, it’s 0. If you run `x & 1`

, Python performs logical and with the bit sequence `y=0000...001`

. For the result, all positions will be `0`

and the last position will be 1 only if `x`

‘s last position is already `1`

which means it is odd.

After converting it using `bool()`

, you still need to invert it using the `not`

operator so that it returns `True`

if the list has an even number of elements.

Here’s an example:

def check_even(my_list): return not bool(len(my_list)&1) print(check_even([])) # True print(check_even([1])) # False print(check_even([1, 2])) # True print(check_even([1, 2, 3])) # False

Bitwise AND is more efficient than the modulo operator so if performance is an issue for you, you may want to use this third approach.

You may want to watch this video on the Bitwise AND operator:

## 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.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn 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 that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a 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.