In Python, is it better to use list comprehensions or for-each loops?

前端 未结 7 1581
盖世英雄少女心
盖世英雄少女心 2020-11-27 18:41

Which of the following is better to use and why?

Method 1:

for k, v in os.environ.items():
       print \"%s=%s\" % (k, v)

Method 2

相关标签:
7条回答
  • 2020-11-27 18:44

    List comprehension is more than twice as fast as explicit loop. Base on Ben James' variation, but replace the x**2 with a more trivial x+2 function, the two alternatives are:

    def foo(n):
      L = []
      for x in xrange(n):
        L.append(x+2)
      return L
    
    
    def bar(n):
      return [x+2 for x in xrange(n)]
    

    Timing result:

    In [674]: timeit foo(1000)
    10000 loops, best of 3: 195 us per loop
    
    In [675]: timeit bar(1000)
    10000 loops, best of 3: 81.7 us per loop
    

    List comprehension wins by a large margin.

    I agree than readability should be a priority over performance optimization. However readability is in the eye of beholder. When I first learn Python, list comprehension is a weird thing I find hard to comprehend! :-O But once I got use to it, it becomes a really nice short hand notation. If you are to become proficient in Python you have to master list comprehension.

    0 讨论(0)
  • 2020-11-27 18:48

    I agree with @Ben, @Tim, @Steven:

    • readability is the most important thing (do "import this" to remind yourself of what is)
    • a listcomp may or may not be much faster than an iterative-loop version... it depends on the total number of function calls that are made
    • if you do decide to go with listcomps with large datasets, it's better to use generator expressions instead

    Example:

    print "\n".join("%s=%s" % (k, v) for k,v in os.environ.iteritems())
    

    in the code snippet above, I made two changes... I replaced the listcomp with a genexp, and I changed the method call to iteritems(). [this trend is moving forward as in Python 3, iteritems() replaces and is renamed to items().]

    0 讨论(0)
  • 2020-11-27 18:50

    If the iteration is being done for its side effect ( as it is in your "print" example ), then a loop is clearer.

    If the iteration is executed in order to build a composite value, then list comprehensions are usually more readable.

    0 讨论(0)
  • 2020-11-27 19:02

    The first one in my opinion, because:

    • It doesn't build a huge string.
    • It doesn't build a huge list (can easily be fixed with a generator, by removing the []).

    In both cases, you access the items in the same way (using the dictionary iterator).

    0 讨论(0)
  • 2020-11-27 19:03

    list comprehensions are supposed to be run at C level, so if there is huge loop, list comprehensions are good choice.

    0 讨论(0)
  • 2020-11-27 19:10

    The particular code examples you have chosen do not demonstrate any advantage of the list comprehension, because it is being (mis-)used for the trivial task of printing. In this simple case I would choose the simple for loop.

    In many other cases, you will want to supply an actual list to another function or method, and the list comprehension is the easiest and most readable way to do that.

    An example which would clearly show the superiority of the list comp could be made by replacing the print example with one involving creating another actual list, by appending to one on each iteration of the for loop:

    L = []
    for x in range(10):
        L.append(x**2)
    

    Gives the same L as:

    L = [x**2 for x in range(10)]
    
    0 讨论(0)
提交回复
热议问题