0 votes
in Python by (30.8k points)
What is list comprehension in Python? Provide an example.

1 Answer

0 votes
by (30.8k points)
List comprehension is used to define and create a list based on an existing list. For example, if we wanted to separate all the letters in the word “retain,” and make each letter a list item, we could use list comprehension:

r_letters = [ letter for letter in 'retain' ]

print( r_letters)


['r', 'e', 't', 'a', 'i', 'n']

Related questions