One Line for Loop in Python

In Python, “for-loop” is widely used to iterate over a sequence, list, or any object. For loop avoids multiple usages of code lines to make the program concise and simple. The “One line for loop” is also the form of “for loop” which means that a complete syntax of “for loop” is written in a single line. The “one line for loop” considers only one line to perform the same operation as the normal “for loop”.

This write-up will provide a deep insight into the one-line for loop in Python. Moreover, the following outcomes are expected:

So, let’s get started!

One Line for Loop in Python

The simplified “for loop” in Python is one line for loop, which iterates every value of an array or list. The one line for the loop is iterated over the “range()” function or other objects like an array, set, tuple, or dictionary.

how to handle linux files and direc...

The syntax of a simple one line for loop is listed below:

Syntax

for <element> in <any_iterable>:
    Code lines....

Let’s discuss the working of one line for loop in Python using the following code:

Code

# one line for loop in Python
for value in range(1, 6):
   print(value)
   
#one line for loop in Python
list_1 = ["b", "a", "s", "h"]

for i in list_1:
    print(i) 

In the above code:

  • There are two “one line for loop” used in the above code.
  • The first “one line for loop” is applied on the range from “(1,6)”, and the print statement is used to print out all the elements in the specified range.
  • In the second part, a variable named “list_1” is initialized, and the “One line for loop” iterates through the elements of “list_1”.

Output

The numbers are printed from “1 to 5,” using the first “one line for loop”. And the characters “b a s h” are printed with the help of a second “one line for loop”.

One Line for Loop in Python Using List Comprehension

In Python, list comprehension is used to create a list from its elements. List comprehension is a written expression with a for loop enclosed inside the square bracket. The Syntax of one line for loop using list comprehension is listed below:

Syntax

My_list = [<expression> <for loop> <condition if any>]

There is no comma (,) or semicolon (;) placed inside the expression of list comprehension or condition of “for loop”.

Let’s understand it with an example of code given below:

Code

list_1 = [6,2,8,3,1]

list_2 = [x+2 for x in list_1]
print(list_2)

In the above code:

  • The list is initialized in a variable “list_1”.
  • The “List comprehension” expression adds the value “2” to each element of the list and stores the answer in the variable named “list_2”.
  • Lastly, the “list_2” is printed.

Output

In the above output, the list elements are added by“2”.

One Line for Loop in Python Using List Comprehension with if-else Statement

The “If else” with “List comprehension” creates more powerful operations like saving space or fast processing repetitive programs. We can perform multiple operations using a single line for loop conditions of list comprehension. The Syntax of one line for loop using list comprehension with if-else is listed below:

Syntax

My_list = [if condition if statement else statement inside else for <element> in <any_iterable>]

In the above syntax, there are two expressions inside the square bracket. one for the “if statement” and another for the “else statement”. The expression value will be iterated on any object using for loop.

Let’s understand “List comprehension with if-else” with an example of the code below.

Code

#one line for loop using if-else list comprehension
list_1 = [1,4,5,8,9,11,13,12]

list_2 = [x+1 if x%2 == 0 else x for x in list_1]
print(list_2)

In the above code:

  • The variable named “list_1” is initialized.
  • The “List comprehension” expression adds the value “1” to each element if the condition is satisfied otherwise “else statement” prints out all the same elements.
  • Lastly, the “list_2” is printed.

Output

The above output has added value“1” in the element of the list whose output divided by 2 is“0”.

That’s all from this Python guide!

Conclusion

In Python, the “one line for loop” is used to perform multiple operations in a single line which reduces the space and amount of code. The “list comprehension” used “one line for loop” to apply an operation on its own elements with the help of the “if condition”. The “list comprehension with if else” conditions are very useful for all the complicated repeated programs code. List comprehension is defined with short expressions rather than complete for loop statements. This write-up covered all the details of various forms of “one line for loop” with the help of some appropriate examples.