How to Create a One-Line For Loop in Python

In Python, a one-line “for” loop is an incredible feature that iterates over the iterable items, like lists, strings, tuples, arrays, etc., to perform a specific task. While working on these data structures, you can use a one-line “for” loop to write your code concisely in a cleaner way.

Although this is another way of using the “for” loop, it is also known as “list comprehension”. It is frequently used to create new lists by comprehensively filtering and transforming the elements of the existing lists. However, many beginners don’t know how to use the one-line “for” loop and sometimes get errors. So, in this tutorial, we will describe the different ways to create a one-line “for” loop in Python.

How to Create a One-Line “For” Loop in Python

One-line “for” loop is the best option when your purpose is to create a list. Besides, you can also use it to perform many other tasks. Let’s now look at the different examples of the one-line loop. Here is the basic syntax:

List = [expression <u>for</u> item <u>in</u> iterable]
  1. The expression is the operation that you want to perform on the items (like x * 3).
  2. The item is the current target element from the iterable.
  3. The iterable is an object that consists of a collection of items that can be iterated using loops.

Example 1: Double the Elements of a List Using the One-Line “For” Loop

If you have an old list and you want to operate on it to double the value of its elements, you can use the following method to do it using the list comprehension.

list_old = [1,2,3,4,5,6]
list_new = [x * 2 for x in list_old]

The “x * 2 for x in list_old” function returns twice every value of “x” from the “list_old”, then stores it in a “list_new”.

Calculate the Square of Elements Using the One-Line “For” Loop

The process to calculate the square of elements is similar to the previous one. But this time, you need to use the following program:

list_old = [1,2,3,4,5,6]
list_new = [x * x for x in list_old]

You can also use x ** 2 instead of x * x which won’t affect the results. Upon compilation, you’ll get the following result:

Filter the Elements of a List Using the One-Line “For” Loop

In this example, let’s use the one-line “for” loop to filter out the cars by their initials.

cars_all = [BMW, Mercedes-Benz, Bentley, Porsche, Lamborghini, Audi, Lexus, Maserati, Aston Martin]
cars_filtered = [word for word in cars_all if word.startswith(‘a’)]

Upon running the code, it returns the cars with the names starting with an “A” as shown in the following image:


This is how you can create a one-line “for” loop to perform numerous tasks in Python. We included multiple examples of the one-line “for” loop so that you can understand everything about the loop. The most important thing to remember is that using a one-line “for” loop requires you to define the sequence of elements that you want to iterate. However, the syntax and usage precisely resemble that of a “for” loop.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.