Reputation: 617
Basically, I am wondering what is the most efficient method to find the elements of a python list with a value of greater than, say, n.
I believe, the easiest, yet not so efficient, way is as below,
for i in range(len(theList)):
if theList[i] > n:
subList.append(theList[i])
Moreover, we have the single line for
as below,
(subList for subList in theList if sublist > n)
(Please correct me if there is anything wrong with the above syntax)
Finally, we can use filter()
function, which is not pleasant to use, at least for me.
The above methods were all the ways that I know. If you know any better method please tell me. Otherwise, please explain which one is the best, in the sense of efficiency and run-time.
Upvotes: 0
Views: 103
Reputation: 10957
There is no always right answer to this and there have been a few SO posts about the speed of different approaches when handling list, see e.g. here, here or here.
What is the fastest way might depend a lot on your list. This said, let's just have a look at how fast the suggested approaches are.
For simple comparisons like this you can use timeit:
1. Case: The for-loop
for_case = """newList=[]
for x in theList:
if x > n:
newList.append(x)"""
2. Case: List comprehension
list_comp = '[x for x in theList if x > n]'
3. Case: The filter (somehow unliked)
filtering = 'list(filter(lambda x: x > n, theList))'
Some preparation:
import timeit
si = 'theList=range(2000);n=1000;' # using list(range(2000)) has no effect on the ranking
So let's see:
timeit.timeit(si+list_comp, number=10000)
Out[21]: 1.3985847820003983
timeit.timeit(si+filtering, number=10000)
Out[22]: 3.315784254024038
timeit.timeit(si+for_case, number=10000)
Out[23]: 2.0093530920275953
So, at least on my machine, the list comprehension takes it away, followed by the for
-loop and, at least in this case the unliked filter
is indeed the slowest.
Upvotes: 2
Reputation: 12100
list comprehension version:
sublist = [ i for i in the_list if i > n ]
Generator expression: ( if the list is huge size)
sublist = ( i for i in the_list if i > n )
Upvotes: -1