This tutorial covers the following topic – Python Add lists. It describes various ways to join/concatenate/add lists in Python. For example – simply appending elements of one list to the tail of the other in a for loop, or using +/* operators, list comprehension, extend(), and itertools.chain() methods.
Most of these techniques use built-in constructs in Python. However, the one, itertools.chain() is a method defined in the itertools module. You must also see which of these ways is more suitable for your scenario. After going through this post, you can evaluate their performance in the case of large lists.
By the way, it will be useful if you have some elementary knowledge about the Python list. If not, please go through the linked tutorial.
Python Add Lists – 6 Ways to Join/Concatenate Lists
For loop to add two lists
It is the most straightforward programming technique for adding two lists.
- Traverse the second list using a for loop
- Keep appending elements in the first list
- The first list would expand dynamically
Finally, you’ll have a single list having all the items from other lists.
# Python Add lists example # Sample code to add two lists using for loop # Test input lists in_list1 = [21, 14, 35, 16, 55] in_list2 = [32, 25, 71, 24, 56] # Using for loop to add lists for i in in_list2 : in_list1.append(i) # Displaying final list print ("\nResult: **********\nConcatenated list using for loop: " + str(in_list1))
Plus (+) operator to merge two lists
Many languages use the + operator for appending/merging strings. Python supports it also, and even for lists.
It is a straightforward merge operation using the “+” operator. And you can easily merge lists by adding one to the back of the other.
# Python merge lists # Sample code to merge two lists using + operator # Test input lists in_list1 = [21, 14, 35, 16, 55] in_list2 = [32, 25, 71, 24, 56] # Apply + operator to merge lists in_list3 = in_list1 + in_list2 # Displaying final list print ("\nResult: **********\nPython merge list using + operator: " + str(in_list3))
Mul (*) operator to join lists
It’s entirely a new method to join two or more lists and is available from Python 3.6. You can apply it to concatenate multiple lists and deliver one unified list.
# Python join two lists # Sample code to join lists using * operator # Test input lists in_list1 = [21, 14, 35, 16, 55] in_list2 = [32, 25, 71, 24, 56] # Apply * operator to join lists in_list3 = [*in_list1, *in_list2] # Displaying final list print ("\nResult: **********\nPython join list using * operator: " + str(in_list3))
List comprehension to concatenate lists
List comprehension allows any operation (concatenation) on the input lists and can produce a new one without much effort. This method processes the list like in a “for” loop, i.e., element by element.
# Python concatenate two lists # Sample code to concatenate lists using list comprehension # Test input lists in_list1 = [21, 14, 35, 16, 55] in_list2 = [32, 25, 71, 24, 56] # Apply list comprehension to concatenate lists in_list3 = [n for m in [in_list1, in_list2] for n in m] # Displaying final list print ("\nResult: **********\nPython concatenate list using list comprehension: " + str(in_list3))
Built-in list extend() method
This function is a part of the Python list class and can also be used to add or merge two lists. It does an in-place expansion of the original list.
# Demonstrate Python Add lists # Sample code to add two lists using list.extend() # Test input lists in_list1 = [21, 14, 35, 16, 55] in_list2 = [32, 25, 71, 24, 56] # Using Python list.extend() method to add lists in_list1.extend(in_list2) # Displaying final list print ("\nResult: **********\nPython Add lists using list.extend(): " + str(in_list1))
itertools.chain() to combine lists
Python itertools chain() function takes multiple iterables and produces a single list. The output is the concatenation of all input lists into one. Let’s illustrate Python join lists with the help of a simple program.
# Sample code to join lists using itertools.chain() import itertools # Test input lists in_list1 = [21, 14, 35, 16, 55] in_list2 = [32, 25, 71, 24, 56] # Using itertools.chain() method to join lists in_list3 = list(itertools.chain(in_list1, in_list2)) # Displaying final list print ("\nResult: **********\nPython join lists using itertools.chain(): " + str(in_list3))
You’ve seen various methods to add/join/merge/concatenate lists in Python. Now, you should evaluate which one fits the most in your scenario. Also, you can now evaluate them based on their speed in case of large data sets. By the way, to learn Python from scratch to depth, do read our step-by-step Python tutorial.