0 votes
in Python by
What is list comprehension in Python? Provide an example.

1 Answer

0 votes
by
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)

Output:

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

Related questions

0 votes
asked Oct 27, 2022 in Python by SakshiSharma
0 votes
asked Feb 11, 2021 in Python by SakshiSharma
...