![]() |
Python List Comprehension. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Python List Comprehension. (/thread-15183.html) |
Python List Comprehension. - rinu - Jan-07-2019 Can you please explain me Python List Comprehension. RE: Python List Comprehension. - buran - Jan-07-2019 Did you check our tutorial on comprehension expressions? Or any other available online? It will be easier to help if you are more specific which part you don't understand. RE: Python List Comprehension. - anjita - Jan-08-2019 The list comprehension is a way to declare a list in one line of code. Let’s take a look at one such example. >>> [i for i in range(1,11,2)] [1, 3, 5, 7, 9] >>> [i*2 for i in range(1,11,2)] [2, 6, 10, 14, 18] RE: Python List Comprehension. - perfringo - Jan-08-2019 As almost everything else in Python it's very close to natural language. This Python code: >>> [x for x in range(10) if x % 2 == 0] [0, 2, 4, 6, 8]translates into English: give me an x for every x in range 0-9 if x is even |