What are the advantages of listcomprehension over loops in Python? Will the performance be higher?

Source: Internet
Author: User
Listcomprehension in python is generally used to calculate another list from one list. It is a combination of mapfilter functions and can be implemented cyclically. Some people have said that list derivation is only syntactic sugar, and that list derivation is more efficient than loop and mapfilter (only one test result is given, no relevant analysis), and other valuable information will not be found... this is a question asked by an interviewer. I 'd like to figure it out, so I know that list comprehension in python is generally used to calculate another list from one list, the function is a combination of map and filter, which can also be implemented through loops. Some people have said that list derivation is only syntactic sugar, and that list derivation is more efficient than loop and map/filter (only one test result is given, no relevant analysis), and other valuable information will not be found... this is a question asked by an interviewer. I 'd like to figure it out, so I 'd like to ask you about it.
In the design philosophy of python, There is a saying "There shoshould be one -- and preferably only one -- obvious way to do it. ", if there is no difference between list derivation, loop, and map/filter implementation, then it will not exist? Of course, in python's philosophy, there is "Beautiful is better than uugly. ", it seems that it may only look good... reply: First of all, we are sure that the efficiency of map and list derivation is indeed higher than that of loops,

Let's start with list derivation. below is my test result in ipython (test environment Python 2.7.10 ):

>>> long_list = range(1000)>>> a = []>>> %timeit for i in long_list: a.append(i+1)10000 loops, best of 3: 100 µs per loop>>> %timeit [i+1 for i in long_list]10000 loops, best of 3: 43.3 µs per loop
A little more efficient. Look at the dis module.

Python does not like FP, and map/filter is inert, and list comprehension is not.

Loops are more underlying than list comprehension, and are hard to understand. Comparison: "multiply all elements in this list by two" and "Create an empty list B. For each element in list, multiply the result by two and add it to the end of B 」.

PS: for every use case that can be implemented in multiple ways, I can basically determine an optimal approach.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.