![]() |
|
[Tut] How to Concatenate Lists in Python? [Interactive Guide] - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Concatenate Lists in Python? [Interactive Guide] (/thread-93953.html) |
[Tut] How to Concatenate Lists in Python? [Interactive Guide] - xSicKxBot - 03-10-2020 How to Concatenate Lists in Python? [Interactive Guide] So you have two or more lists and you want to glue them together. This is called list concatenation. How can you do that? These are six ways of concatenating lists:
Code: a = [1, 2, 3]
b = [4, 5, 6] # 1. List concatenation operator +
l_1 = a + b # 2. List append() method
l_2 = [] for el in a: l_2.append(el) for el in b: l_2.append(el) # 3. List extend() method
l_3 = []
l_3.extend(a)
l_3.extend(b) # 4. Asterisk operator *
l_4 = [*a, *b] # 5. Itertools.chain()
import itertools
l_5 = list(itertools.chain(a, b)) # 6. List comprehension
l_6 = [el for lst in (a, b) for el in lst]Output: Code: '''
l_1 --> [1, 2, 3, 4, 5, 6]
l_2 --> [1, 2, 3, 4, 5, 6]
l_3 --> [1, 2, 3, 4, 5, 6]
l_4 --> [1, 2, 3, 4, 5, 6]
l_5 --> [1, 2, 3, 4, 5, 6]
l_6 --> [1, 2, 3, 4, 5, 6] '''What’s the best way to concatenate two lists? If you’re busy, you may want to know the best answer immediately. Here it is: To concatenate two lists Code: l1Code: l2Code: l1.extend(l2)To concatenate more than two lists, use the unpacking (asterisk) operator Code: [*l1, *l2, ..., *ln]You’ll find all of those six ways to concatenate lists in practical code projects so it’s important that you know them very well. Let’s dive into each of them and discuss the pros and cons. 1. List Concatenation with + Operator If you use the + operator on two integers, you’ll get the sum of those integers. But if you use the + operator on two lists, you’ll get a new list that is the concatenation of those lists. Code: l1 = [1, 2, 3]
l2 = [4, 5, 6]
l3 = l1 + l2
print(l3)Output: Code: [1, 2, 3, 4, 5, 6]The problem with the + operator for list concatenation is that it creates a new list for each list concatenation operation. This can be very inefficient if you use the + operator multiple times in a loop. Performance: How fast is the + operator really? Here’s a common scenario how people use it to add new elements to a list in a loop. This is very inefficient: Code: import time start = time.time() l = []
for i in range(100000): l = l + [i] stop = time.time() print("Elapsed time: " + str(stop - start))Output: Code: Elapsed time: 14.438847541809082The experiments were performed on my notebook with an Intel® Core i7-8565U 1.8GHz processor (with Turbo Boost up to 4.6 GHz) and 8 GB of RAM. I measured the start and stop timestamps to calculate the total elapsed time for adding 100,000 elements to a list. The result shows that it takes 14 seconds to perform this operation. This seems slow (it is!). So let’s investigate some other methods to concatenate and their performance: 2. List Concatenation with append() The Code: list.append(x)Code: xCode: listHere’s a similar example that shows how you can use the Code: append()Code: l1Code: l2Code: l1Code: l1 = [1, 2, 3]
l2 = [4, 5, 6]
for element in l2: l1.append(element)
print(l1)Output: Code: [1, 2, 3, 4, 5, 6]It seems a bit odd to concatenate two lists by iterating over each element in one list. You’ll learn about an alternative in the next section but let’s first check the performance! Performance: I performed a similar experiment as before. Code: import time start = time.time() l = []
for i in range(100000): l.append(i) stop = time.time() print("Elapsed time: " + str(stop - start))Output: Code: Elapsed time: 0.006505012512207031I measured the start and stop timestamps to calculate the total elapsed time for adding 100,000 elements to a list. The result shows that it takes only 0.006 seconds to perform this operation. This is a more than 2,000X improvement compared to the 14 seconds when using the + operator for the same problem. While this is readable and performant, let’s investigate some other methods to concatenate lists. 3. List Concatenation with extend() You’ve studied the Code: append()You bet there is! The method Code: list.extend(iter)Code: iterCode: listThe difference between Code: append()Code: extend()Here’s a similar example that shows how you can use the Code: extend()Code: l1Code: l2Code: l1 = [1, 2, 3]
l2 = [4, 5, 6]
l1.extend(l2)
print(l1)Output: Code: [1, 2, 3, 4, 5, 6]The code shows that the Code: extend()Code: append()But is it also fast? Let’s check the performance! Performance: I performed a similar experiment as before for the Code: append()Code: import time start = time.time() l = []
l.extend(range(100000)) stop = time.time() print("Elapsed time: " + str(stop - start))Output: Code: Elapsed time: 0.0I measured the start and stop timestamps to calculate the total elapsed time for adding 100,000 elements to a list. The result shows that it takes negligible time to run the code (0.0 seconds compared to 0.006 seconds for the Code: append()The Code: extend()But there’s still more… 4. List Concatenation with Asterisk Operator * There are many applications of the asterisk operator (read this blog article to learn about all of them). But one nice trick is to use it as an unpacking operator that “unpacks” the contents of a container data structure such as a list or a dictionary into another one. Here’s a similar example that shows how you can use the asterisk operator to concatenate two lists Code: l1Code: l2Code: l1 = [1, 2, 3]
l2 = [4, 5, 6]
l3 = [*l1, *l2]
print(l3)Output: Code: [1, 2, 3, 4, 5, 6]The code shows that the asterisk operator is short, concise, and readable (well, for Python pros at least). And the nice thing is that you can also use it to concatenate more than two lists (example: Code: l4 = [*l1, *l2, *l3]But how about its performance? Performance: You’ve seen before that Code: extend()Code: extend()Code: import time l1 = list(range(0,1000000))
l2 = list(range(1000000,2000000)) start = time.time()
l1.extend(l2)
stop = time.time() print("Elapsed time extend(): " + str(stop - start))
# Elapsed time extend(): 0.015623092651367188 start = time.time()
l3 = [*l1, *l2]
stop = time.time() print("Elapsed time asterisk: " + str(stop - start))
# Elapsed time asterisk: 0.022125720977783203I measured the start and stop timestamps to calculate the total elapsed time for concatenating two lists of 1,000,000 elements each. The result shows that the extend method is 32% faster than the asterisk operator for the given experiment. To merge two lists, use the extend() method which is more readable and faster compared to unpacking. Okay, let’s get really nerdy—what about external libraries? 5. List Concatenation with itertools.chain() Python’s itertoolsmodule provides many tools to manipulate iterables such as Python lists. Interestingly, they also have a way of concatenating two iterables. After converting the resulting iterable to a list, you can also reach the same result as before. Here’s a similar example that shows how you can use the Code: itertools.chain()Code: l1Code: l2Code: import itertools l1 = [1, 2, 3]
l2 = [4, 5, 6]
l3 = list(itertools.chain(l1, l2))
print(l3)Output: Code: [1, 2, 3, 4, 5, 6]The result is the same. As readability is worse than using extend(), append(), or even the asterisk operator, you may wonder: Is it any faster? Performance: You’ve seen before that Code: extend()Code: extend()Code: import time
import itertools l1 = list(range(2000000,3000000))
l2 = list(range(3000000,4000000)) start = time.time()
l1.extend(l2)
stop = time.time() print("Elapsed time extend(): " + str(stop - start))
# Elapsed time extend(): 0.015620946884155273 start = time.time()
l3 = list(itertools.chain(l1, l2))
stop = time.time() print("Elapsed time chain(): " + str(stop - start))
# Elapsed time chain(): 0.08469700813293457I measured the start and stop timestamps to calculate the total elapsed time for concatenating two lists of 1,000,000 elements each. The result shows that the Code: extend()Code: itertools.chain()Here’s my recommendation: Never use Code: itertools.chain()Code: extend()So let’s go back to more Pythonic solutions: everybody loves list comprehension… 6. List Concatenation with List Comprehension List comprehension is a compact way of creating lists. The simple formula is Code: [ expression + context ]
The example Code: [x for x in range(3)]Code: [0, 1, 2]Need a refresher on list comprehension? Read this blog tutorial. Here’s a similar example that shows how you can use list comprehension to concatenate two lists Code: l1Code: l2Code: l1 = [1, 2, 3]
l2 = [4, 5, 6]
l3 = [ x for lst in (l1, l2) for x in lst ]
print(l3)Output: Code: [1, 2, 3, 4, 5, 6]What a mess! You’d never write code like this in practice. And yet, tutorials like this one propose this exact same method. The idea is to first iterate over all lists you want to concatenate in the first part of the context Code: lst in (l1, l2)Code: for x in lstDo we get a speed performance out of it? Performance: You’ve seen before that Code: extend()Code: extend()Code: import time l1 = list(range(4000000,5000000))
l2 = list(range(6000000,7000000)) start = time.time()
l1.extend(l2)
stop = time.time() print("Elapsed time extend(): " + str(stop - start))
# Elapsed time extend(): 0.015620946884155273 start = time.time()
l3 = [ x for lst in (l1, l2) for x in lst ]
stop = time.time() print("Elapsed time list comprehension: " + str(stop - start))
# Elapsed time list comprehension: 0.11590242385864258I measured the start and stop timestamps to calculate the total elapsed time for concatenating two lists of 1,000,000 elements each. The result shows that the Code: extend()Summary To concatenate two lists l1, l2, use the Code: l1.extend(l2)To concatenate more than two lists, use the unpacking operator Code: [*l1, *l2, ..., *ln]Want to use your skills to create your dream life coding from home? Join my Python freelancer webinar and learn how to create your home-based coding business. Click: How to Become a Python Freelancer? |