List comprehension is nothing but a shorter and crisper version of the code and also memory efficient. By using this we can either create a new list or perform some operation in an existing list.

The normal code for creating a list of 0–9 will be like

x=[]
for i in range (10):
x.append(i)
print(x)
[0,1,2,3,4,5,6,7,8,9]

By using list comprehension

x=[i for i in range(10)]
print(x)

[0,1,2,3,4,5,6,7,8,9]

As you can see the normal code is long but the code that we did using list comprehension does the job just in one line so list comprehension is preferred over the traditional method.

#list-comprehension #lists #python #python-list-comprehension

List Comprehension
1.35 GEEK