[Python] List Append Performance: += > extend > append

I have a Python script that runs a little slowly and I was wondering how I could improve the performance. It is a fairly long for loop where we're trying to find matches in a large list. In the loop we're appending to a list with +=.

Q: Could I improve the performance with .append() or .extend()?

The Test Definition

def test(number):
  from datetime import datetime
  count = 0; a = []
  start = datetime.now)_
  while count != number:
    a += ['TESTING1234']
    count += 1
  end = datetime.now()
  print(end - start)
  print(f'Rows in list = {len(a)}')

The Results

I ran the test 3 times for each of +=, .append() and .extend() and the fastest was -


- which I was already using.

The results for test(5000000) were fairly consistent with averages:

  1. += 7 seconds
  2. .extend() 8 seconds
  3. .append() 26 seconds
+= and extend() are very close, but += just edges it. append() is very slow.