import timeit

# List creation needs to be inside the timing loop for a fair timing
# of the mutative option, so we time the list creation and subtract the
# time from the other timings.

list_creation = r'''
a = [0, 1] * 7
'''

using_loop = list_creation + r'''
for n, i in enumerate(a):
    if i == 1:
        a[n] = 10
'''

using_comprehension = list_creation + r'''
b = [10 if x==1 else x for x in a]
'''

list_creation_time = timeit.timeit(list_creation, number=1000000)
loop_time = timeit.timeit(using_loop, number=1000000) - list_creation_time
comprehension_time = timeit.timeit(using_comprehension, number=1000000) - list_creation_time

print("Loop time:         ", loop_time)
print("Comprehension time:", comprehension_time)