What is the syntax for writing a for loop on one line in Python? This syntax is known as a list comprehension and enables the user to write a for loop on one lin
To write a for loop on one line in Python, known more commonly as the
, wrap the for loop in a list like so:
[elem for elem in my_loop]
Here is an example demonstrating how this code works:
>>> my_list = [1, 2, 3] >>> [elem for elem in my_list] [1, 2, 3]
As you can see from the above example the output is exactly the same as the input but demonstrates the point that the inline for loop as detailed works.
Without being wrapped in a list the code simply will not work:
>>> my_list = [1, 2, 3] >>> for elem in my_list File "<stdin>", line 1 for elem in my_list ^ SyntaxError: invalid syntax
As you can see from the output above the Python REPL shows it is expecting something more at the end of the one line for loop (being the colon) and therefore reports an error of
Combine One-Liner For Loop With Conditions
Another handy feature of the one-liner for loop is that it also permits the use of conditions both before and after the for loop section.
placed has its own particulars on what happens to each element in the for loop.
If conditions are place after the for loop this filters the elements that are captured and inserted into the new list .
For example, if I wanted to filter a list and capture only items that were odd numbers the condition placed after the list is preferred. Here’s a demonstration:
>>> my_list = [1, 2, 3] >>> [elem for elem in my_list if elem % 2 > 0] [1, 3]
Notice in the example above how the new list gives us a reduced quantity of elements (2) compared to the original list which had 3. Therefore, this technique filters out elements from the list that do not satisfy the criteria of the conditions after the for loop.
What if there were conditions placed before the for loop?
The difference with conditions placed before the for loop compared to the conditions being placed after the for loop is that there is retained the same quantity of elements to the original list.
For example, you cannot remove an element from the new list by placing an if statement before the for loop – here are some examples showing the results:
>>> my_list = [1, 2, 3] >>> [elem if elem % 2 > 0 for elem in my_list] File "<stdin>", line 1 [elem if elem % 2 > 0 for elem in my_list] ^ SyntaxError: invalid syntax
The only syntax that will work is the proper one line if statement which has the format:
value_if_true if expression else value_if_false
Therefore, there will need to be a false value if the condition is not true.
>>> my_list = [1, 2, 3] >>> [elem if elem % 2 > 0 else None for elem in my_list] [1, None, 3]
Notice how in the result of this list the second element is given the result of
as defined in the
section of the one line if statement.
The one line for loop is an excellent way of looping through a list using one line of code. When looping through the list using the for loop, you can also insert conditions either before or after the for loop to help control the output of the elements in the new list.