Path: blob/master/Python core/Day 3Loops and List Comprehension in Python.ipynb
3074 views
Loops
List Comprehension in Python
List comprehension saves time because it requires fewer lines of code than loops. It makes the code more user-friendly by keeping it simple. Furthermore, list comprehension covers the incorporation of an iterative statement into a formula.
Python provides several methods for creating lists. The list comprehension feature is one of the most effective of these methods. It allows you to create lists with just one line of code
It can be used to create new lists from other iterable elements such as arrays, strings, tuples, lists, and so on. It consists of brackets containing the expression. To iterate over all of the elements, the system uses the for loop to execute the expression for each one
When you use list comprehension instead of loops, you can create a list with far less effort and code.
List comprehension isn’t just for integers; it can also be applied to strings.
The for loop is a common way to iterate through a list. List comprehension, on the other hand, is a more efficient way to iterate through a list because it requires fewer lines of code.
List comprehension requires less computation power than a for loop because it takes up less space and code. This is useful when working on large programs, as efficiency becomes a major consideration when the code is lengthy.
Questions for Practice!!
1. Finding the elements in a list in which elements are ended with the letter ‘d’ and the length of that element is greater than 2.
2. Reverse each String in a Tuple using List Comprehension
Dictionary comprehension
Dictionary comprehension allows you to create dictionaries in Python, as the name implies.