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)
I agree with @Ben, @Tim, @Steven:
Example:
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 toitems()
.]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.
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 thefor
loop:Gives the same
L
as:The first one in my opinion, because:
[]
).In both cases, you access the items in the same way (using the dictionary iterator).
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:
-- 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:
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:
Timing result:
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.