1

I am creating a new list from a large old list based on a certain value being not equal to None. Which method of iteration is faster?

Option 1:

new_list = []
for values in old_list:
   if values[4] is not None:
       new_list.append(values[4])

Option 2:

new_list = [x for x in old_list if x[4] is not None]
4
  • 7
    You could test this for yourself using the timeit module. Commented Dec 30, 2014 at 19:00
  • 3
    I have a request. Please don't put a space between new_list and .append. I couldn't find anything in the Python style guide specifically forbidding it, so just consider it a personal favor to me ;-) Commented Dec 30, 2014 at 19:15
  • @Kevin - favor granted! Commented Dec 30, 2014 at 19:20
  • Option 2 is faster - I tested this sort of thing 5 years ago on python2.7. Back then, it was about 10x faster Commented Dec 30, 2014 at 20:08

2 Answers 2

5

Try to timeit both.

But second is widely known to be faster.

Basically map is faster than list comprehension which is faster than for loop.

A whole lot of literature is available on the web about this subject.

EDIT:

I promised an update with actual, tangible results. Here is the code.

import random
import timeit

old_list = [ random.randint(0, 100000) for i in range(0, 100) ]


def floop(old_list):
    new_list = []
    for value in old_list:
        new_list.append(value)
    return new_list


def lcomp(old_list):
    new_list = [ value for value in old_list ]
    return new_list


if __name__=='__main__':
    results_floop = timeit.Timer('floop(old_list)', "from __main__ import floop, old_list").timeit()
    results_lcomp = timeit.Timer('lcomp(old_list)', "from __main__ import lcomp, old_list").timeit()
    print("Function\t\tSeconds elapsed")
    print("For loop\t\t{}".format(results_floop))
    print("List comp\t\t{}".format(results_lcomp))

Remember: timeit loops through the called function 1 million times and prints the time elapsed in seconds. Read it like to execute this 1 million times, it took xx seconds.

Here are the results. I think they speak by themselves.

~/python » python3 lists.py
Function        Seconds elapsed
For loop        11.089475459069945
List comp       5.985794545034878
Sign up to request clarification or add additional context in comments.

3 Comments

You might want to put in some timeit results of your own, in case the link ever rots.
@Ffisegydd you're right - will be updating this answer soon with some results
Cool, I did not know about timeit. I chose this as a best answer, but would still like to see the example.
1

The second is both faster, and more readable. If you need more speed, and are only iterating through the result once, you could use filter - new_list = filter(lambda x: x[4] is not None, old_list). You could call list on the filtered result, but that might not have any speed advantage over the list comprehension (and is less Pythonic IMHO).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.