Skip to content

How to Write Python For Loop in One Line? Vijetha Spark By {Examples}

  • by

Python provides various ways to writing for loop in one line. For loop in one line code makes the program more readable and concise. You can use for loop to iterate through an iterable object or a sequence which is the simplest way to write a for loop in one line. You can use simple list comprehension and list comprehension with an if-else statement to write the for loop in one-line code.

In this article, I will explain the following ways of Python to write a for loop in one-line code with examples.

Simple One Line For Loop

Using List Comprehension

List Comprehension with if-else statement

Using nested For Loops

Nested For Loop with condition

Nested For Loop with Multiple conditions

1. Quick Examples of Writing For Loop in One Line

Following are quick examples of writing for loop in one-line code.

# Quick Examples of writing for loop in one line.

# Example 1: Write For loop in one line
# to iterate through a list
my_list = [“Python”, “Pandas”, “Spark”, “PySpark”]
print(“My list :”, my_list)
for item in my_list: print(item)

# Example 2: Write For loop in one line
# to iterate through a range
for item in range(0, 5): print(item)

# Example 3: Get one line for loop using list comprehension.
my_list = [2, 3, 4, 5]
new_list = [item**3 for item in my_list]

# Example 4: Get one line for loop using list comprehension with
# if-else statement
my_list = [5, 11, 20, 19, 25]
new_list = [item for item in my_list if item % 5 == 0]

# Example 5: Get one line for loop using list comprehension with
# if-else statement
new_list = [True if item % 5 == 0 else False for item in my_list]

# Example 6: Get one line for loop using list comprehension with
# nested loops
# Initialize the lists
list1 = [5, 10]
list2 = [1, 2, 3]
new_list = [x*y for x in list1 for y in list2 ]

# Example 7: Get one line for loop using list comprehension with
# nested loops & if condition
new_list = [x*y for x in list1 for y in list2 if x % 2 == 0]

# Example 8: Get one line for loop using list comprehension with
# nested loops & multiple conditions
new_list = [x*y for x in list1 for y in list2 if x % 2 == 0 if y % 2 != 0]

2. Simple One Line For Loop in Python

Use for loop to iterate through an iterable object such as a list, set, tuple, string, dictionary, etc., or a sequence. This iteration process is done in one-line code this is the basic way to write for loop in one line. 

Let’s implement a one-line for loop to iterate over Python iterable objects. In this example, I will take the list of strings as an iterable object.

# Write For loop in one line
# to iterate through a list
# Initialize the list
my_list = [“Python”, “Pandas”, “Spark”, “PySpark”]
print(“My list :”, my_list)
for item in my_list: print(item)

Yields below output.

You can use the range() function to write one-line for loop in Python. Let’s get the one-line for loop to iterate through a range(0, 5) function.

# Write For loop in one line
# to iterate through a range
for item in range(0, 5): print(item)

Yields below output.

3. Get One Line Python For Loop Using List Comprehension

You can use list comprehension to get the for loop in one line. A list comprehension is a concise and elegant way to create a new list from an existing list in Python.

With list comprehension, you can create a new list by applying specific operations on each element of the existing list. All this is done in a for loop one-line code.

# Get one line for loop using list comprehension
# Initialize the list
my_list = [2, 3, 4, 5]
print(“My list:”, my_list)
new_list = [item**3 for item in my_list]
print(“New list:”, new_list)

# Output:
# My list: [2, 3, 4, 5]
# New list: [8, 27, 64, 125]

4. List Comprehension with if-else in One Line For Loop

You can also get the one-line for loop to use List comprehension with a if-else statement. Simply use list comprehension with a if-else statement, you can create a new list or filter elements from the existing list by applying specific conditions over the elements of the existing list. All this is done in one line code for loop.

# Get one line for loop using list comprehension with
# if-else statement
# Initialize the list
my_list = [5, 11, 20, 19, 25]
print(“My list:”, my_list)
new_list = [item for item in my_list if item % 5 == 0]
print(“New list:”, new_list)

# Output:
# My list: [5, 11, 20, 19, 25]
# New list: [5, 20, 25]

Using the if-else condition inside list comprehension is a powerful way to filter and modify the list based on certain conditions. Let’s apply the if-else condition inside the list comprehension to get the modified list by taking elements from the existing list.

# Get one line for loop using list comprehension with
# if-else statement
# Initialize the list
my_list = [5, 11, 20, 19, 25]
print(“My list:”, my_list)

new_list = [True if item % 5 == 0 else False for item in my_list]
print(“New list:”, new_list)

# Output:
# My list: [5, 11, 20, 19, 25]
# New list: [True, False, True, False, True]

5. Python Nested For Loop in One Line

So far, we have learned how to implement for loop in a one-line code. Now, we will learn how to implement nested loops in one-line code. A loop inside another loop is called a nested for loop.

Using nested loops in list comprehension you can perform operations on multiple lists at the same time. It involves using multiple for loops in a single list comprehension, allowing you to iterate over multiple lists and create a new list based on the values of the input lists. All this is done in one line of code.

Here is an example of using two lists and combining them using two for loops:

# Get one line for loop using list comprehension with
# nested loops
# Initialize the lists
list1 = [5, 10]
list2 = [1, 2, 3]

print(“My list1:”, list1)
print(“My List2:”, list2)

new_list = [x*y for x in list1 for y in list2 ]
print(“New list:”, new_list)

# Output:
# My list1: [5, 10]
# My List2: [1, 2, 3]
# New list: [5, 10, 15, 10, 20, 30]

6. Python Nested For Loop with the condition in One Line

You can use nested for loops in list comprehension to create a list in a one-line code based on condition. Using nested loops along with conditions within a list comprehension you can create a list by applying specific conditions on elements of existing lists. All this is done in a one-line code.

# Get one line for loop using list comprehension with
# nested loops & if condition
new_list = [x*y for x in list1 for y in list2 if x % 2 == 0]
print(“New list:”, new_list)

# Output:
# My list1: [5, 10]
# My List2: [1, 2, 3]
# New list: [10, 20, 30]

7. Python Nested For Loop with Multiple Conditions in One Line

You can use nested for loops in list comprehension to create a list in a one-line code based on multiple conditions. Using nested loops along with multiple conditions within a list comprehension you can create a list by applying some conditions on elements of existing lists. All this is done in a one-line code.

# Get one line for loop using list comprehension with
# nested loops & multiple conditions
new_list = [x*y for x in list1 for y in list2 if x % 2 == 0 if y % 2 != 0]
print(“New list:”, new_list)

# Output:
# My list1: [5, 10]
# My List2: [1, 2, 3]
# New list: [10, 30]

8. Conclusion

In this article, I have explained the very basic way to implement Python for loop in one-line code through an iterable or sequence. Also explained using list comprehension, list comprehension with conditions, nested loops, and nested loops with conditions how we can implement the for loop in one line code with examples.

Happy Learning!!

 Python provides various ways to writing for loop in one line. For loop in one line code makes the program more readable and concise. You can use for loop to iterate through an iterable object or a sequence which is the simplest way to write a for loop in one line. You can use simple list  Read More Python, Python Tutorial, Pending review by admin, Python For Loop Examples 

Leave a Reply

Your email address will not be published. Required fields are marked *