Speeding Up Creating Multiple Empty Lists in Python

list_len = len(old_list)
list = [None] * list_len


list = [None] * len(old_list)

Use Case:

When I create DataFrames, I’ll create a series of lists to fill. The DataFrame is created by using lists. reviews is a list of HTML blocks. Here’s how I would have normally set up the lists.

texts = [None] * len(reviews)
dates = [None] * len(reviews)
ratings = [None] * len(reviews)
checkin = [None] * len(reviews)
photos = [None] * len(reviews)

A quick timing shows that texts = [None] * len(reviews) is about 22% slower than using a defined variable as the length of the list.

Time to create five empty lists with a length of 20:

  • Using review_len: 772 ns ± 5.66 ns
  • Using len(reviews): 969 ns ± 13.5 ns
  • Using 20: 706 ns ± 14 ns

I’m surprised to see that using a definitive length, 20, didn’t save more time.

Saving nanoseconds may seem like a trivial amount of time, but continually attempting to optimize code is admirable.