Articles → Python → List Comprehension In PythonList Comprehension In PythonIn this article, we will discuss about list comprehension in pythonPurpose List comprehension is a concise way of creating list.Example Consider a scenario where you want to create a list. The simplest logic you can think of isout = [] for x in range(1, 5): out.append(x) print(out)There is another way to do it using list comprehension. So the above logic can we rewritten asout = [] out = [x for x in range(1,5)] print (out)In both the cases, output is same.Click to EnlargePosted By - Karan Gupta Posted On - Monday, March 11, 2019 Query/Feedback Your Email Id Subject Query/Feedback Characters remaining 250
out = [] for x in range(1, 5): out.append(x) print(out)
out = [] out = [x for x in range(1,5)] print (out)
Query/Feedback