An introduction to List Comprehension
python is eminent for encouraging developers and Data Scientist write efficient, easy to understand code and a simple to read code as well
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.
syntax:
newList =[ expression(element) for element in oldList if condition ]
Advantages of List Comprehension
Require fewer lines of code
Transforms iterative statement into a formula
More time efficient and space efficient than loops
Iterating through a String using List comprehensions
h_letters = [letter for letter in "letter"]
print(h_letters)
When we run the program, the output will be:
['l','e','t','t','e','r']
in the above example, a new list is assigned to variable h_letters, and list contains the items of the iterable string 'letters'
we type the print() function to call the output
Comments