Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Comprehensions
#1
Hello,

I've recently discovered List Comprehensions - which, for the project I am working on, are awesome.
I'm having to create lists based on other content that gets  'filtered' with for loops / if statements.

A lot of people mention List Comprehensions being 'easier to read'.
Honestly, I think the feature is cool, but I much prefer the readability of the old fashioned way - actually nesting this stuff inside of their for loops / if statements.
For some reason, having everything broken down like that is just easier for me to follow and understand what the program is doing.

So - 
A) Though both yield the same result, is one way faster than the other?
If in fact they take the same amount of time to process, then what is the benefit of doing the comprehension - just the 'readability'?

B) In the 'real world' of using Python - is it frowned upon to build lists not using the list comprehension feature (assuming execution time is the same)?

At first glance, this seems like a Personal Preference vs Actual Performance battle.
Reply
#2
It's my understanding that list comprehensions are more efficient than full for loops. Since they are more limited, they don't require as much processor overhead as a full for loop. I have not tested this to confirm it.

I expect most people who think list comprehensions are easier to read really just believe they're easier to type.

If you're not doing anything big enough to worry about efficiency, it's personal preference. I'm sure there are people who would look down their noses at you using a full for loop, but I don't think such noses are worth looking down.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Quote:A) Though both yield the same result, is one way faster than the other?
LC is faster.

Quote:B) In the 'real world' of using Python - is it frowned upon to build lists not using the list comprehension feature (assuming execution time is the same)?
LC is used everywhere in Python.
There also a functional program way,but LC is looked upon as more pythonic.
Eg:
>>> lst = ['1', '500', '999']
>>> [int(i) for i in lst]
[1, 500, 999]

# Functional program way
>>> list(map(int, lst))
[1, 500, 999]
Quote:At first glance, this seems like a Personal Preference vs Actual Performance battle.
Not so much a Performance thing,LC has become a natural part of the Python language.
Reply
#4
In addition you can use list comprehensions wherever lists are expected (parameter to a function call, for instance), so this allows you to create ad-hoc lists on the fly.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
Hello,

On performance, this from wiki.python.org:
Quote:Python supports a couple of looping constructs. The for statement is most commonly used. It loops over the elements of a sequence, assigning each to the loop variable. If the body of your loop is simple, the interpreter overhead of the for loop itself can be a substantial amount of the overhead. This is where the map function is handy. You can think of map as a for moved into C code. The only restriction is that the "loop body" of map must be a function call. Besides the syntactic benefit of list comprehensions, they are often as fast or faster than equivalent use of map

Larz60+
Reply
#6
so does all this also apply to DCs?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
Quote:is it frowned upon to build lists not using the list comprehension feature (assuming execution time is the same)?
Rather the opposite in complex cases. People dont like complex/long and drawn out list comps that take longer to decipher what it does, than to just use regular for loops thats easier to understand.
Recommended Tutorials:
Reply
#8
The performance difference is small,if want to know just use timeit.
Here are all 3 and also tested with PyPy.:cool: 
import timeit

ordinary_loop = '''\
lst = ['1', '500', '999']
l = []
for i in lst:
    l.append(int(i))
'''
# 21.14 sec
# PyPy 3 sec

list_comp = '''\
lst = ['1', '500', '999']
[int(i) for i in lst]
'''
# 20.54 sec
# PyPy 2.59 sec

_map = '''\
lst = ['1', '500', '999']
list(map(int, lst))
'''
# 22.6 sec
# PyPy 3.5 sec

print(timeit.Timer(stmt=_map).timeit(number=10000000))
Reply
#9
If the size of the list is increased it makes the results of timeit show that comprehensions are faster

import timeit
ordinary_loop = '''
lst = range(10000000)
l = []
for i in lst:
    l.append(int(i))
'''
list_comp = '''
lst = range(10000000)
[int(i) for i in lst]
'''
_map = '''
lst = range(10000000)
list(map(int, lst))
'''

print(timeit.Timer(stmt=ordinary_loop).timeit(number=10))
print(timeit.Timer(stmt=list_comp).timeit(number=10))
print(timeit.Timer(stmt=_map).timeit(number=10))
Output

42.300742557436585
30.58364898989899
23.1211604744974
Reply
#10
Hello,

Soooo... looks like the wiki.python.org post is incorrect!
bad wiki ... No soup for you!

Larz60+
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  abusing comprehensions for one line loops Skaperen 6 2,527 Dec-16-2019, 11:22 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020