Python List ComprehensionIntroduction:In this tutorial, we are discussing list comprehension in Python. Python is known for helping us produce code that is elegant, simple to write, and reads almost as well as plain English. List comprehension is one of the language's most distinguishing features, allowing us to develop sophisticated functionality with just one line of code. On the other hand, many Python writers struggle to fully utilize the more complex aspects of list comprehension. Sometimes programmers may overuse them, resulting in much less efficient and difficult-to-read code. Example: Here we give the basic example of List comprehension in Python. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - ['Piyali', 'Hiya', 'Rudrashish', 'Lipi'] Syntax: The syntax of List comprehension in Python is given below - Here we are showing the basic use of list comprehension. Program Code: Now we give code examples without using list comprehension; how can we square every number of a list using just a for loop in Python? The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - [9, 25, 1, 49, 9, 81] This can be accomplished with only a single line of code using list comprehension. Program Code: Now we give code examples of list comprehension in Python for square every number of a list. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - [9, 25, 1, 49, 9, 81] Benefits of Using List ComprehensionsThere are so many advantages or benefits of using list comprehension in Python. The advantages are given below - 1. Loops and maps: Loops and maps are typically regarded as more Pythonic than list comprehensions. But rather than taking that judgment at face value, it is worth considering the advantages of utilizing a list comprehension in Python over the alternatives. We will learn about a couple of cases when the alternatives are preferable options later. 2. The Single tool uses: One of the essential advantages of utilizing list comprehension in Python is that it is a single tool that can be used in various circumstances. We don't need to adopt a new strategy for each situation. List comprehensions may be leveraged for mapping or filtering and basic list generation. 3. Does not depend on appropriate parameter: List comprehensions are regarded as Pythonic, as Python emphasizes simple, effective tools that can be used in many scenarios. As a bonus, we won't have to remember the appropriate order of parameters when using a list comprehension in Python, as we would when calling map(). 4. Easy to use: List comprehensions are easier to read and grasp than loops since they are more declarative. We must concentrate on how exactly the List is constructed while using loops. We must manually build an empty list, loop over the List's entries, and add each one to the List's end. Instead, using a list comprehension in Python, we can concentrate on what we want to put in the List and allow Python to handle the list generation. Program Code: Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - Time taken by for loop: 7.005999803543091 Time taken by list comprehension: 2.822999954223633 Using List Comprehension to Iterate through StringList comprehension can also be used in the case of strings, as they are iterables. Program Code: Now we give code examples of using list comprehension in Python to iterate a string given in code. The code is given below - Output: ['j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't'] Using Conditions in List ComprehensionConditional statements can be used by list comprehensions to change existing lists (or other tuples). We'll make a list with mathematical operators, numbers, and a range of values. Program Code: Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29] Addition of odd elements to the List:Here we sum the odd elements from the given List. It is an example of list comprehension in Python. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - [11, 15, 10, 16, 6, 17, 10] Nested List ComprehensionsNested List Comprehensions are similar to nested for loops in that they are a list comprehension inside another list comprehension. The programme that implements nested loop is as follows: Program Code: Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - [[0, 1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6]] The same result may be created in fewer lines of code by utilizing layered list comprehensions. Program Code: The code for nested list comprehension is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - [[0, 1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6]] List comprehension is a powerful tool for describing and creating new lists based on existing ones. In general, list comprehension is lighter and easier to use than traditional list construction functions and loops. To provide user-friendly code, we should avoid writing large codes for list comprehensions. Every interpretation of the list or other iterables can be recast in a for loop, but not all the for loops can be rebuilt in the framework of list comprehension. Next TopicConvert Roman Number to Decimal (Integer) | Write Python Program to Convert Roman to Integer |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India