
listone = [2, 3, 4]
listtwo = [2*i for i in listone if i > 2] print listtwo
Here, we derive a new list by specifying the manipulation to be done ( 2*i) when some condition is satisfied (if i > 2). Note that the original list remains unmodified. Many a time, we use loops to process each element of a list, the same can be achieved using list comprehensions in a more precise, compact and explicit manner.
Describe what you're looking for in as much detail as you'd like.
Our AI reads your request and finds the best matching books for you.
Popular searches:
Join 2 million readers and get unlimited free ebooks