Today we will use the timeit module to try to find out the fastest way to combine a couple of Lists in Python.
We will be considering the following different ways to combine lists
- Using the + operator
- Using a for loop
- Using append()
- Using extend()
- Unpacking with * operator
- Using itertools.chain()
How are we going to measure the performance?
lst1 and lst2 are two lists with 1000000 random integers, we have created a function that will combine the lists in the different approaches mentioned above. This function will be called 10 times and we will measure the average time taken for each approach.
Using the + operator
def func(lst1 , lst2):
lst3 = lst1 + lst2
Time taken
On average it took 0.01051248973235488 seconds
Output
I have used smaller lists to show the output
Input 1: [10, 3, 24, 56, 7]
Input 2: [100, 100, 200, 40, 60]
Output: [10, 3, 24, 56, 7, 100, 100, 200, 40, 60]
Using a for loop
def func(lst1 , lst2):
lst3 = []
for element in lst1:
lst3.append(element)
for element in lst2:
lst3.append(element)
Time taken
On average it took 0.09956733733415604 seconds
Output
Input 1: [10, 3, 24, 56, 7]
Input 2: [100, 100, 200, 40, 60]
Output: [10, 3, 24, 56, 7, 100, 100, 200, 40, 60]
Using append()
Note: this is in-place, i.e unlike our previous approaches, we do not create a new list. Instead lst1 is updated
def func(lst1 , lst2):
lst1.append(lst2)
Time taken
On average it took 6.137415766716003e-07 seconds
Output
Input 1: [10, 3, 24, 56, 7]
Input 2: [100, 100, 200, 40, 60]
After append()
Input 1: [10, 3, 24, 56, 7, [100, 100, 200, 40, 60]]
Input 2: [100, 100, 200, 40, 60]
As you can see, lst1 get's updated. Also notice unlike the previous approaches, we are not directly combining the elements of both the list. lst1 contains lst2 as an element
Using extend()
This is similar to append. It is an in-place method. The only difference being instead of adding the entire lst2 to lst1, it adds the elements of lst2 to lst1
def func(lst1 , lst2):
lst1.extend(lst2)
Time taken
On average it took 0.01660564970225096 seconds
Output
Input 1: [10, 3, 24, 56, 7]
Input 2: [100, 100, 200, 40, 60]
After extend()
Input 1: [10, 3, 24, 56, 7, 100, 100, 200, 40, 60]
Input 2: [100, 100, 200, 40, 60]
Notice the difference in Input 1
After append()
Input 1: [10, 3, 24, 56, 7, [100, 100, 200, 40, 60]]
After extend()
Input 1: [10, 3, 24, 56, 7, 100, 100, 200, 40, 60]
Unpacking with * operator
def func(lst1 , lst2):
lst = [*lst1,*lst2]
Time taken
On average it took 0.031204084493219854 seconds
Output
Input 1: [10, 3, 24, 56, 7]
Input 2: [100, 100, 200, 40, 60]
Output: [10, 3, 24, 56, 7, 100, 100, 200, 40, 60]
Using itertools.chain()
def func(lst1 , lst2):
lst3 = itertools.chain(lst1,lst2)
lst3 = list(lst3)
The chain() function returns an iterator object. We can convert it to a list by using the list() function.
Time taken
On average it took 0.028354898421093823 seconds
Output
Input 1: [10, 3, 24, 56, 7]
Input 2: [100, 100, 200, 40, 60]
Output: [10, 3, 24, 56, 7, 100, 100, 200, 40, 60]
Comparison of the approaches
append() is the fastest but it doesn't combine the elements of both the lists. The + operator seems to be the ideal option. However, this has been done on a comparatively smaller dataset and results may vary when you try it on your own.
Knowing different approaches to combine list is pretty useful and can you help you optimize your code
Top comments (0)