How to do Single Line for Loop in Python?

A single-line for loop is also known as a list comprehension. It allows you to iterate over a sequence, apply an expression or transformation to each element, and create a new list in a concise one-line syntax.

Here’s the general syntax for a single-line for loop or list comprehension in Python:

new_list = [expression for item in sequence]Code language: Python (python)

To create a single-line for loop, you need to follow these steps:

  • Define the expression or transformation you want to apply to each item.
  • Specify the loop variable (item) that represents each element of the sequence.
  • Provide the sequence you want to iterate over.

Here’s a similar example that demonstrates the use of a single-line for loop with a conditional statement:

fruits = ['apple', 'banana', 'orange', 'kiwi', 'mango']
selected_fruits = [fruit.upper() for fruit in fruits if len(fruit) > 5]
print(selected_fruits)Code language: Python (python)


['BANANA', 'ORANGE']Code language: Python (python)

In this example, the single-line for loop iterates over each fruit in the fruits list. The conditional statement if len(fruit) > 5 filters out fruits that have a length greater than 5 characters.

The filtered fruits are then transformed to uppercase using the upper() method, and the resulting list of selected fruits is stored in the selected_fruits list.

Finally, the selected_fruits list is printed as the output, showing the uppercase names of the fruits that met the condition.

What is a for loop in one line called?

A for loop written in one line is commonly known as a “list comprehension” in Python. List comprehensions provide a concise way to create lists by iterating over a sequence, applying an expression or transformation to each element, and collecting the results in a new list. They are often used when you want to perform a simple operation on each element of a sequence and generate a new list based on that operation.

List comprehensions can also include conditional statements to filter the elements or apply additional conditions. This allows for more complex transformations or filtering within a single line of code.

In addition to list comprehensions, Python also has other similar constructs, such as set comprehensions and dictionary comprehensions, which follow a similar syntax and allow you to create sets and dictionaries in a concise manner.

However, it’s worth noting that while list comprehensions provide a compact and expressive way to write simple for loops, it’s important to balance conciseness with code readability. In some cases, using a regular for loop with multiple lines may be more appropriate, especially for complex operations or when the code needs to be more easily understood by others.

Read More;

  • Dmytro Iliushko

    I am a middle python software engineer with a bachelor's degree in Software Engineering from Kharkiv National Aerospace University. My expertise lies in Python, Django, Flask, Docker, REST API, Odoo development, relational databases, and web development. I am passionate about creating efficient and scalable software solutions that drive innovation in the industry.

Leave a Comment