Python one line for loop tutorial

To create a one line for loop in Python, you can use one of the following methods:

  1. If the for loop body is simple, you can write the statement next to the colon
  2. If you’re creating a list, use a list comprehension
  3. If you have an if condition, use a conditional list comprehension

This tutorial shows you how to create one line for loops in practice.

1. One liner for loop

If you have a simple for loop, then you can write the loop body in the same line as the loop header:

for condition: do_something

Let’s see a practical example. Suppose you have a for loop as follows:

for i in range(5):
    print(i)

The code above can be converted to a single line for loop as follows:

for i in range(5): print(i)

If you have multiple statements, you can separate them into a single line using semicolons:

# This code:
for i in range(5):
    print("Start")
    print(i)
    print("End")

# Equivalent to:

for i in range(5): print("Start"); print(i); print("End")

But keep in mind that writing a single line for loop goes against Python conventions of one statement per line.

Only use this syntax if you have a strong reason to.

2. Using a list comprehension and for loop

The for loop can be combined with a list comprehension to create a list.

Suppose you want to add a +2 to a range of numbers from 1 to 5. Here’s how you do it using a regular for loop:

result = []

for i in range(1, 6):
    result.append(i + 2)
    
print(result)

# [3, 4, 5, 6, 7]

You can shorten the code above by using a list comprehension as follows:

result = [i + 2 for i in range(1, 6)]

print(result)

# [3, 4, 5, 6, 7]

Isn’t that simple? List comprehension allows you to create a list and do operations on the list items in one line. You also don’t have to declare a list variable before running the list comprehension.

Next, let’s see how to add an if statement in a list comprehension

3. Conditional list comprehension

If you want to create a list that fulfills a certain condition, you can add an if statement to the list comprehension.

For example, say you want to create a list of even numbers from a range. This is how you do it with a regular for loop:

even = []

for number in range(1, 11):
    if number % 2 == 0:
        even.append(number)

print(even)

# [2, 4, 6, 8, 10]

Now here’s how you do the same task using a conditional list comprehension:

even = [number for number in range(1, 11) if number % 2 == 0]

print(even)

# [2, 4, 6, 8, 10]

It’s a bit nauseating, but essentially the list comprehension uses a for loop to iterate over a range of numbers. Each number that returns True in the if check gets added to the list.

Aside from being hard to read, a conditional list comprehension also has limited support for else statements.

Suppose you want to create a new list that squares each even number in the range as follows:

squared = []

for number in range(1, 11):
    if number % 2 == 0:
        squared.append(number ** 2)
    else:
        squared.append(number)

print(squared)

# [1, 4, 3, 16, 5, 36, 7, 64, 9, 100]

You can create a one line conditional comprehension as follows:

numbers = range(1, 11)

squared = [num ** 2 if num % 2 == 0 else num for num in numbers]

print(squared)
#  [1, 4, 3, 16, 5, 36, 7, 64, 9, 100]

But suppose you want to create a two list out of the numbers. Let’s say you want to create two lists, one for even numbers and another for odd numbers:

odd = []
even = []

for number in range(1, 11):
    if number % 2 == 0:
        even.append(number)
    else:
        odd.append(number)

print(odd)
print(even)

# [1, 3, 5, 7, 9]
# [2, 4, 6, 8, 10]

The example above uses two lists, and it can’t be done using a conditional comprehension.

The best you can do is to use the ternary operator and put the if-else statement in one line as follows:

odd = []
even = []

for number in range(1, 11):
    even.append(number) if number % 2 == 0 else odd.append(number)

print(odd)
print(even)

# [1, 3, 5, 7, 9]
# [2, 4, 6, 8, 10]

You can even write the for loop body in one line like this:

for number in range(1, 11): even.append(number) if number % 2 == 0 else odd.append(number)

But of course, this is discouraged in practice as it’s hard to read. Don’t blame me if someone scolded you for it. 😬

Conclusion

This tutorial has shown you examples of writing a one line for loop in Python.

Writing a one line for loop goes against Python code conventions that state you must not have more than one statement per line. But if you have a really simple for loop condition in your project, then by all means go for it.

I hope this tutorial is helpful. Happy coding! 👍

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.