Problem Formulation
Given a list in Python. How to check if the list has an odd number of elements?
Examples:
[] --> False
[1] --> True
[1, 2] --> False
[1, 2, 3] --> True
Related Article:
Method 1: len() and Modulo
The most Pythonic way to check if a list has an odd 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 odd number of elements use the expression len(my_list)%2==1
.
Here’s a simple code example:
def check_odd(my_list): return len(my_list)%2==1 print(check_odd([])) # False print(check_odd([1])) # True print(check_odd([1, 2])) # False print(check_odd([1, 2, 3])) # True
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 odd 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 odd value 1 to a boolean, use the built-in bool()
function around the result, i.e., bool(len(my_list)%2)
.
Here’s a simple code example:
def check_odd(my_list): return bool(len(my_list)%2) print(check_odd([])) # False print(check_odd([1])) # True print(check_odd([1, 2])) # False print(check_odd([1, 2, 3])) # True
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.
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.
Here’s an example:
def check_odd(my_list): return bool(len(my_list)&1) print(check_odd([])) # False print(check_odd([1])) # True print(check_odd([1, 2])) # False print(check_odd([1, 2, 3])) # True
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.