List Comprehension in Python

Tram Ho

List comprehension allows us to create a list containing data with only one line. Instead of creating an empty list, repeating some magic data, and adding it to the list on all separate lines, we can use comprehension to perform these steps at the same time. It does not increase performance, but it does cleaner and helps reduce the lines of code inside the program. Together with comprehension we can reduce two or more lines into a single line.

List Comprehension Syntax

The syntax when using list comprehension depends on what you are trying to write. The general play structure to understand list comprehension is as follows:

For example, when you want to create a list, the syntax will be structured as follows:

However, when you want with an if statement, the comprehension will look like this:

Item will only be added to the list if the if condition is met. Finally, if you want to add an else statement, it will look like this:

When using the else statement in the list comprehension, the first item will only be added to the list when the if statement is True. If it is False, then the item following the else statement will be added to the list.

Generating a List of Numbers

Let’s try creating a list of numbers from 0 to 100 using the list comprehension:

You may have noticed that a printed list of 100 numbers was printed. List comprehension allows you to build this list with a single line instead of writing a for statement and adding separate lines. Take a look at the usual spelling:

As you can see, I reduced the three lines to one by comprehension. This does not improve performance but reduces the number of lines in the code. It becomes clearer in larger programs, and I recommend trying to use comprehension when possible.

If Statements

Earlier, I looked at how to change the syntax when using an if statement in comprehension. Let’s try an example by creating a list of even numbers only:

For comprehension, the variable x is only added to the list when the condition is True . In my case, the condition is True when the current value of x is divisible by 2. The following, you will find the same code needed without using comprehension:

This time, I can reduce four lines of code to one line. This can often improve your readability of the code.

If-Else Statements

Now, let’s go one step further and add an else condition. This time, I will append the string “Even” when the number is divisible by two; If not, we will concatenate the “Old” string:

This will print out a list of strings representing odd numbers or even values. Here I add the string “Even” even if the condition is true; if not, another command will be hit and add the string “Old” . Showing the same code without using it can be seen as follows:

I reduced the code lines from six to one. Understands very well for quick data creation; However, it becomes more difficult when the conditions are larger. Comprehensions do not allow the use of elif statements, only if / else.

List Comprehension with Variables

Comprehension is great for creating data from lists. Create a list of numbers and create a separate list for those squares, using comprehension:

I will get a list [4, 16, 36, 64] . In this example, I was able to generate square numbers by adding the expression “num ∗∗ 2” . The representation of the code without using comprehension will be as follows:

Dictionary Comprehension

Not only can you use comprehension in lists, but dictionaries are also great. The syntax structure is identical, except that you need a key-value pair instead of a unique number to add to the dictionary. Let’s create a dictionary of even numbers as a key and squares as values:

I will get the following dictionary: {0: 0, 2: 4, 4: 16, 6: 36, 8: 64} .

References Projects for Beginners.pdf

Share the news now

Source : Viblo