Python line break in list comprehension

List comprehensions are often used in Python to write single line statements that create a new list or dictionary by looping over an iterable object. This article will explain how to use list comprehensions in Python, starting with a basic explanation of how for loops work in Python.

For Loop in Python

A for loop statement in Python sequentially iterates over members of any object, list, string etc. Compared with other programming languages, its syntax is much cleaner and doesnt require manually defining iteration steps and starting iteration. Though there are ways to make its behavior the same as other programming languages [wont be covered in this article]. You can also exercise some control over for loops by using statements like continue, break, pass etc. Below is a simple example of for loop in Python:

for x in range[10]:
print[x]

The for loop above will print ten numbers starting from 0 and ending at 9.

List Comprehensions

List comprehension is nothing but a shorthand / concise way to write multi-line for loops in a one-liner statement. The list comprehension example below will create a new list as [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] by including all values of x in it.

numbers = [x for x in range[10]]
print [numbers]

Note that list comprehension always creates a new list and doesnt modify original iterables used in the expression. A typical list comprehension expression must have a for clause and can be followed by if and else conditional statements. Without using a list comprehension, the example above will be written in following manner:

numbers = []
for x in range[10]:
numbers.append[x]

print [numbers]

Performance and Readability

List comprehensions are faster than for loops. However, unless you are iterating over hundreds of thousands of items, you wont notice major performance improvements. While list comprehension provides a concise way to write for loops, complex expressions can lead to poor readability of code and increased verbosity. It is important to keep code readable, unless achieving maximum performance is an absolute necessity for your program.

Example: Using List Comprehensions Syntax with Dictionaries and Sets

A python dictionary is a collection of elements defined in key-value pairs while a set is a collection of unique values where duplicates are not allowed. List comprehensions can be used with Python dictionaries and sets as well. The syntax differs slightly, instead of wrapping up the expression in square braces, you will now have to use curly braces. You will also get a new dictionary / set object instead of a new list.

data = {"city": "new york", "name": "john doe"}

formatted_data = {k: v.title[] for k,v in data.items[]}

print [formatted_data]

The example above will convert string values to title case and create a new dictionary called formatted_data, whose output will be: {city: New York, name: John Doe}. You can also change the dictionary / set in-place by specifying the existing dictionary variable on the left hand side.

data = {"city": "new york", "name": "john doe"}

data = {k: v.title[] for k,v in data.items[]}

print [data]

Without using dictionary comprehensions, the code would look like this:

data = {"city": "new york", "name": "john doe"}

formatted_data = {}

for k, v in data.items[]:
formatted_data[k] = v.title[]

print [formatted_data]

As there are no key-value pairs in sets, a set comprehension can be defined in the same way as a list comprehension. The only difference is the use of curly braces.

Example: Multiple For Loops in a List Comprehension

The list comprehension example mentioned above is basic and uses a single for statement. Below is an example that uses multiple for loops and a conditional if statement.

adjectives = ["Disco", "Eoan", "Focal", "Artful"]

animals = ["Dingo", "Ermine", "Fossa", "Beaver"]

codenames = [x + " " + y for x in adjectives for y in animals if y.startswith[x[0]]]

print [codenames]

The code will show [Disco Dingo, Eoan Ermine, Focal Fossa] as output. The two for loops go over adjectives and animals lists and their members are joined together using a space, only if the first letter of both the words are the same. Without using list comprehensions, the code would look like this:

adjectives = ["Disco", "Eoan", "Focal", "Artful"]
animals = ["Dingo", "Ermine", "Fossa", "Beaver"]

codenames = []

for x in adjectives:
for y in animals:
if y.startswith[x[0]]:
codenames.append[x + " " + y]

print [codenames]

Example: List Comprehension with if-else Clause

The example below will show usage of if and else statements in list comprehensions.

number_list = [1, 2, 3, 4]
another_list = [5, 6, 7, 8]

result = [True if [x + y] % 2 == 0 else False for x in number_list for y in another_list]

print [result]

While looping through two lists, the list comprehension above checks if the sum of the pair of elements is even or not. Running the code above will show you [True, False, True, False, False, True, False, True, True, False, True, False, False, True, False, True] as output. Without using list comprehension, the code would look like this:

number_list = [1, 2, 3, 4]
another_list = [5, 6, 7, 8]
result = []

for x in number_list:
for y in another_list:
if [x + y] % 2 == 0:
result.append[True]
else:
result.append[False]

print [result]

Conclusion

List comprehensions provides a nice way to write clean and concise loop statements. However, they can quickly get complex and difficult to understand if multiple loops and conditional statements are used. In the end, it comes to the comfort level of a programmer but generally it is a good idea to write explicit, readable, and easy to debug code instead of excessively using shorthands.

Video liên quan

Chủ Đề