Python

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]
print(list_new)

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]
print(list_new)

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’)]
print(cars_filtered)

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

Conclusion

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.