Posts: 24
Threads: 8
Joined: Sep 2016
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.
Posts: 4,229
Threads: 97
Joined: Sep 2016
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.
Posts: 7,087
Threads: 122
Joined: Sep 2016
Oct-01-2016, 06:38 PM
(This post was last modified: Oct-01-2016, 06:46 PM by snippsat.)
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.
Posts: 687
Threads: 37
Joined: Sep 2016
Oct-01-2016, 09:20 PM
(This post was last modified: Oct-02-2016, 01:32 PM by Ofnuts.)
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
Posts: 11,871
Threads: 474
Joined: Sep 2016
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+
Posts: 4,558
Threads: 1,463
Joined: Sep 2016
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.
Posts: 5,150
Threads: 396
Joined: Sep 2016
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:
Posts: 7,087
Threads: 122
Joined: Sep 2016
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))
Posts: 2,164
Threads: 35
Joined: Sep 2016
Oct-02-2016, 11:39 AM
(This post was last modified: Oct-02-2016, 11:40 AM by Yoriz.)
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
Posts: 11,871
Threads: 474
Joined: Sep 2016
Hello,
Soooo... looks like the wiki.python.org post is incorrect!
bad wiki ... No soup for you!
Larz60+
|