How to Create a One-Line For Loop in Python

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

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

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

A one-line “for” loop is the best choice when your objective is to create a list. Apart from this, you can also use it to perform many other tasks. Let us now look at various examples of one-line loops. Here is the basic syntax:

List , [expression <u>for</u> item <u>in</u> iterable]

  1. The expression is the operation you want to perform on the item (e.g. x * 3).
  2. Item is the current target element from the iterable.
  3. Iterable is an object containing a collection of items that can be iterated over using a loop.

Example 1: Double the elements of a list using a one-line “for” loop

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

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

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

Calculate the square of elements using a one-line “for” loop

The process of calculating the class of elements is similar to the previous one. But this time, you have to use the following programs:

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

You can also use x ** 2 in place of x * x which will not affect the results. When compiled, you will get the following results:

Filter elements of a list using a one-line “for” loop

In this example, let's use a one-line “for” loop to filter cars by their starting letters.

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

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

conclusion

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

Add comment

By Ranjan