Create a List with the short syntax: List comprehension
In the previous blogs, I have discussed python lists, how to create lists! Why to use a list? You had also seen how to use a for loop?
You know there are many ways to create a list in python. You can create a list with the help of the for loop, map and list constructor.
These methods are useful to create a python list. Although these methods only applicable to particular condition.
In the above example, you can see that it’s easy to write the code. But it irritates the same process again and again.
To solve this problem python community introduced List comprehension in python 2.0.
So, With the help of the list comprehension, you can do this task in one line of code.
In the above example, you can see that How easily I had done instead of using for loop.
So, what the meaning of comprehension?
Comprehension is used here to means complete inclusion.
In this blog, you are going to see why we use List comprehension? What is the meaning of the list comprehension? And how to use List comprehension?
What is list comprehension?
The term list comprehension comes when you have to create a new list based on the old list.
With the help of the list comprehension, you can do expression, iterable(dict, set, list) in one line of code.
You don’t need to use the for loop.
The syntax of the list comprehension is
new_list = [expression for item in iterable if condition == True]
The return value of list comprehension is a new list leaving the old list unchanged.
Condition accept the item first if it is true. A condition statement is optional. You can remove it.
The iterable is list, dictionary, set and tuple. This iterable is an old list in which working you have to create a new one.
The expression in the list comprehension returns the current items with functionality. You can apply the method to expression.
You can manipulate the expression before it ends.
Expression with condition
In this section, you are going to see how to put the condition with expression.
So, why I’m putting a condition in expression.
I’m putting a condition in the expression because I have to apply expression in a particular condition.
Here, you can see that how I had applied the condition in the expression.
You can apply the if-else in expression.
In the above example, you can see that I have defined the list and its functionality at the same time.
There are many benefits of the list that you can use in a list comprehension.
Benefits of list comprehension
List comprehension is a single tool that you can use in many situations.
For example, you can use it in mapping and filtering.
In it, you don’t have to remember the order of the argument.
It’s a declarative statement that means easy to read and use.
The benefit of list comprehension is that you have only to focus on what you want. For output, you can trust python.
In the case of speed, list comprehension is faster than for loop.
The main disadvantage of list comprehension is that programmers use it when code is easy.
But you have to use list comprehension when the code is harder to understand.
One of the reasons you are using the list comprehension to make a clean and shortcode.
If the logic is too long then you shouldn’t use list comprehension.
In list comprehension, you can only gain a list. Not other iterable such as tuple, list and dictionary.
If you want clear and shortcode you can use the list comprehension.
With the help of list comprehension, you can execute code much faster than the solution using the map and lambda.
Although, there are many disadvantages of list comprehension. Mostly when the logic is too long you shouldn’t use list comprehension.
You can use list comprehension in many different situations.
You can’t use tuple comprehension because the tuple is not using in for loop.
Thanks for reaching this point. I hope you loved it. If you found it interesting then share it. If you found something misinform you can tell me.
For more, you can see
"Quality brings clarity"
- A new way to format the string in Python 3.6: fstring - October 11, 2021
- Ultimate new guide for python beginners in 2021 - October 3, 2021
- How to download famous thonny easily for python? - September 20, 2021