39

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:

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

I tend to lead towards the first as more understandable, but that might just be because I'm new to Python and list comprehensions are still somewhat foreign to me. Is the second way considered more Pythonic? I'm assuming there's no performance difference, but I may be wrong. What would be the advantages and disadvantages of these 2 techniques?

(Code taken from Dive into Python)

1
  • 10
    Print is slow anyway - go for readability (for loop). In general, go for readability first. Then, when it seems slow - profile and optimize. Unless you are designing a framework (library), you can get away with smelly code at first. Then you will get a feel for what works and what does not. May 17, 2010 at 14:06

7 Answers 7

44

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.

6
  • 7
    +1: list comprehensions hail from functional programming, while for-loops hail from imperative programming.
    – badp
    May 17, 2010 at 16:36
  • I agree about readability, but readable is not the primary consideration in real world code. maybe in demo code readability is king. performance is sometimes king in the real world, although not always. generators are used, when used, because they confer some benefit. The OP asked "which is better". Which ever gets the job done in a manifestly better way (less wasteful of resources in some cases, faster in some cases, as your requirements dictate).
    – Warren P
    May 17, 2010 at 17:43
  • 5
    I would initially go for readability unless there's a reason not to. In the example given, there's no reason to try to speed up the print by a few microseconds. In the more general case, if you're iterating for the side effects of the expression, it's quite likely that the side effect producing statement takes more time than the iteration construct you use. May 17, 2010 at 18:01
  • 1
    As the saying goes, premature optimization is the root of all evil. Write it to be readable, that way, when you can test the finished program and actually figure out where optimizations are needed, you understand what the code is doing.
    – mklauber
    Apr 1, 2011 at 13:21
  • @badp: And using list comprehensions for everything makes functional languages like Haskell very hard to read.
    – endolith
    Nov 9, 2013 at 19:15
27

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)]
2
  • the problem with your example is that it is trivial. Why not actually use the code in the question to show the differences? Possibly with some timing info? May 17, 2010 at 14:36
  • Another benefit is that you can supply the resulting list to another function, or chain/nest comprehensions.
    – klochner
    May 17, 2010 at 17:07
16

I find the first example better - less verbose, clearer and more readable.

In my opinion, go with what best gets your intention across, after all:

Programs should be written for people to read, and only incidentally for machines to execute.

-- from "Structure and Interpretation of Computer Programs" by Abelson and Sussman

By the way, since you're just starting to learn Python, start learning the new String Formatting syntax right away:

for k, v in os.environ.items():
    print "{0}={1}".format(k, v)
1
  • 1
    please note that str.format() is present only in python ver >= 2.6
    – Nas Banov
    Jun 13, 2010 at 7:05
15

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.

4
  • Exactly, with list comprehensions you are writing more high-performance code, which will be understood by advanced pythonistas, and not so much, by the first-year students.
    – Warren P
    May 17, 2010 at 17:44
  • list comprehensions express the actual logic better, close to set builder notation. A for loop has to be "executed in the head" to see what it does, in general. May 18, 2010 at 0:47
  • I haven't tried in actual Python, but your example code looks as if the performance gain could be made arbitrarily large by increasing n: Unless Python does something to counter this effect, foo uses an O(n^2) algorithm, while bar runs in O(n). May 19, 2010 at 6:41
  • foo is O(n) just as bar. Python's list.append is O(1). May 19, 2010 at 14:11
4

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).

3

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

2

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().]

3
  • Why is readability the MOST important thing, always, for everyone?
    – Warren P
    May 17, 2010 at 17:44
  • it's a pay-it-forward type of mentality that makes such code "Pythonic...," being kind and respectful of your fellow programmers, esp. since your code will likely outlive you at your place of employment. it's hard to explain well in words. again, check out "import this"... the Zen of Python gives you general guidelines as they pertain to Python's philosophy.
    – wescpy
    May 18, 2010 at 0:27
  • 2
    @Warren: Because readability leads to maintainability and readability makes performance improvements that much easier. Never forget to concentrate on those parts where your profiler points to when optimizing. May 19, 2010 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.