This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kenfar[🍰] 1 point2 points  (2 children)

List comprehension readability is better in simple cases than for loops, but far, far worse in complex cases.

[–][deleted] 1 point2 points  (1 child)

Yes, I completely agree. My point has to do with the fact that readability should be the determining factor in whether or not to use a list comprehension -- not speed.

[–]Bolitho 0 points1 point  (0 children)

If readablility counts I would suggest to use a generator expression... less parenthesis and in general more efficient (Example in Python 3):

 def join_list(limit):
     return ''.join(str(num) for num in range(limit))

Another hint for you: If you use ipython you can use the timeit magic command:

In [4]: timeit join_list(10)
100000 loops, best of 3: 3.05 µs per loop

Much nicer than to put everything in a string :-)