Python List ComprehensionPython is renowned for encouraging developers and programmers to write efficient, easy-to-understand, and almost as simple-to-read code. One of the most distinctive aspects of the language is the python list and the list compression feature, which one can use within a single line of code to construct powerful functionality. Show
List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc. A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics. To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course Syntax:
Advantages of List Comprehension
List Comprehensions vs For LoopThere are various ways to iterate through a list. However, the most common approach is to use the for loop. Let us look at the below example: Python3
Output:
Above is the implementation of the traditional approach to iterate through a list, string, tuple, etc. Now list comprehension does the same task and also makes the program more simple. List Comprehensions translate the traditional iteration approach using for loop into a simple formula hence making them easy to use. Below is the approach to iterate through a list, string, tuple, etc. using list comprehension. Python3
Output:
The list comprehensions are more efficient both computationally and in terms of coding space and time than a for loop. Typically, they are written in a single line of code. The below program depicts the difference between for loops and list comprehension based on performance. Python3
Output: Time taken for_loop: 0.56 Time taken for list_comprehension: 0.47From the above program, we can see list comprehensions are quite faster than for loop. Nested List ComprehensionsNested List Comprehensions are nothing but a list comprehension within another list comprehension which is quite similar to nested for loops. Below is the program which implements nested loop: Python3
Output:
Now by using nested list comprehensions same output can be generated in fewer lines of code. Python3
Output:
List Comprehensions and LambdaLambda Expressions are nothing but shorthand representations of Python functions. Using list comprehensions with lambda creates an efficient combination. Let us look at the below examples: Python3
Output: [10, 20, 30, 40, 50]Here, we have used for loop to print table of 10. Python3
Output: [10, 20, 30, 40, 50]Now here, we have used only list comprehension to display table of 10. Python3
Output: [10, 20, 30, 40, 50]Finally, we use lambda + list comprehension to display the table of 10. This combination is very useful to get efficient solutions in fewer lines of code for complex problems. Conditionals in List ComprehensionWe can also add conditional statements to the list comprehension. We can create a list using range(), operators, etc. and cal also apply some conditions to the list using the if statement. Below are some examples which depict the use of list comprehensions rather than the traditional approach to iterate through iterables: Example 1: Display square of numbers from 1 to 10.Python3
Output: [4, 16, 36, 64, 100]Example 2: Display even elements from a list of random numbers.Python3
Output: [++++[+10, 40, 70], [20, 50, 80], [30, 60, 90]]Example 3: Toggle case of each character in a string.Python3
Output:
Example 4: Reverse each string in a tuple.Python3
Output: ['skeeG', 'rof', 'skeeG']Example 5: Display the sum of digits of all the odd elements in a list.Python3
Output: [16, 3, 18, 18]Key Points
Article Tags :
Python Technical Scripter
python-list Technical Scripter 2020 Practice Tags :
python-list |