List comprehension vs a for loop

Report a typo

Everyone says that list comprehension is faster than a for loop. You decide to check that with timeit and write the following code:

import timeit
cycle = """l = []
for num in range(1, 100):
    l.append(num)
"""
comprehension = "[num for num in range(1, 100)]"
timeit.timeit(cycle, number=10000)
timeit.timeit(comprehension, number=100000)

However, the code doesn't work the way it should. You look through it and find a mistake. What is that mistake?

Select one option from the list
___

Create a free account to access the full topic