Python concatenate lists | combine & merge lists | 8 methods


Written by - Deepak Prasad

In this tutorial we will explore different methods to combine lists in Python. This can also be referred as concatenating two or more lists, or merging multiple lists into one object.

 

Method-1: Python concatenate lists using + operator

We can combine multiple lists into a new object using traditional + operator.

 

Example-1: Concatenate multiple lists using + operator

Here in this example I have 3 lists with me and we will combine each of these to create a new object using + operator:

#!/use/bin/env python3
# Python Concatenate Lists - Example-1

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
list_3 = [7, 8, 9]

# Join all the 3 lists into one new object
merged_list = list_1 + list_2 + list_3

# print the elements of merged_list object
print(merged_list)

Output from this script:

~]# python3 example-1.py
[1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Method-2: Python combine lists using list.extend() method

We can use python's list.extend() method to extend the list by appending all the items from the iterable.

 

Example-2: Append lists to the original list using list.extend()

In this Python example we have two lists, where we will append the elements of list_2 into list_1 using list.extend().

#!/use/bin/env python3
# Python Concatenate Lists - Example-2

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]

# Append list_2 element into list_1
list_1.extend(list_2)

# print the elements of list_1
print(list_1)

As expected, the list_1 now contains content of both list_1 and list_2. Output from this script:

~]# python3 example-2.py
[1, 2, 3, 4, 5, 6]

 

Example-3: Combine multiple lists and create a new list using list.extend()

In the previous example, we appended the content of list_2 into list_1. But we can also join both the lists and create a new object with the combined elements.

#!/use/bin/env python3
# Python Concatenate Lists - Example-3

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]

# Define a new list
merged_list = []

# Use list.extend() to append all the lists element to merged_list
merged_list.extend(list_1)
merged_list.extend(list_2)

# print the elements of merged_list object
print(merged_list)

Output from this script:

~]# python3 example-3.py
[1, 2, 3, 4, 5, 6]

 

Method-3: Python join lists using * expression

This method can be used only if you are using python >= 3.5. Use the following command to check your Python version:

 ~]# python3 -V
Python 3.6.8

 

Example-4: Join lists using * operator

This PEP 0448 proposes extended usages of the * iterable unpacking operator to allow unpacking in more positions, an arbitrary number of times, and in additional circumstances. Specifically, in function calls, in comprehensions and generator expressions, and in displays.

#!/use/bin/env python3
# Python Concatenate Lists - Example-4

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
list_3 = [7, 8, 9]

# unpack all iterables in a list literal
merged_list = [*list_1, *list_2, *list_3]

# print the elements of merged_list object
print(merged_list)

Output from this script:

~]# python3 example-4.py
[1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Method-4: Python concatenate lists using itertools.chain()

You can also combine lists using <a href="https://docs.python.org/3/library/itertools.html#itertools.chain" target="_blank" rel="noopener">itertools.chain()</a> function. It makes an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted.

#!/use/bin/env python3
# Python Concatenate Lists - Example-5

import itertools

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
list_3 = [7, 8, 9]

# merge all iterables in a different list object
merged_list = list(itertools.chain(list_1, list_2, list_3))

# print the elements of merged_list object
print(merged_list)

Output from this script:

~]# python3 example-5.py
[1, 2, 3, 4, 5, 6, 7, 8, 9]

 

Method-5: Use list comprehension to merge lists in Python

A List comprehensions allows you to generate this same list in just one line of code. It combines the for loop and the creation of new elements into one line, and automatically appends each new element.

#!/use/bin/env python3
# Python Concatenate Lists - Example-6

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]

merged_list = []
for x in list_1, list_2:
    for y in x:
        merged_list.append(y)

# print the elements of merged_list object
print(merged_list)

Output from this script:

 ~]# python3 example-6.py
[1, 2, 3, 4, 5, 6]

Now we can write the Python for loop which we used in this example into a single line code:

#!/use/bin/env python3

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]

merged_list = [ y for x in [list_1, list_2] for y in x ]

# print the elements of merged_list object
print(merged_list)

 

Method-6: Append lists using list.append() function

We already discussed about list.extend() function. Similarly we have list.append() function in Python which is used to add an item to the end of the list.

We can use a for loop to iterate over a list and append each element into another list.

#!/use/bin/env python3
# Python Concatenate Lists - Example-7

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]

# Define a new list
merged_list = []

# Append list_1 elements into merged_list
for i in list_1:
    merged_list.append(i)

# Append list_2 elements into merged_list
for i in list_2:
    merged_list.append(i)

# print the elements of merged_list object
print(merged_list)

Output from this script:

 ~]# python3 example-7.py
[1, 2, 3, 4, 5, 6]

 

Method-7: Python combine lists using operator module

You can also use the operator module for lists concatenation which works similar to + operator.

#!/use/bin/env python3
# Python Concatenate Lists - Example-8

import operator

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]

merged_list = operator.add(list_1, list_2)

# print the elements of merged_list object
print(merged_list)

Output from this script:

~]# python3 example-8.py
[1, 2, 3, 4, 5, 6]

 

Method-8: Python concatenate lists using yield from expression

PEP 380 adds the yield from expression, allowing a generator to delegate part of its operations to another generator. This allows a section of code containing yield to be factored out and placed in another generator.

itertools.chain() also internally uses the same method to merge lists but for more complex environment, you can directly use yield from to combine lists.

#!/use/bin/env python3
# Python Concatenate Lists - Example-9

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]

def merge(l1, l2):
    yield from l1
    yield from l2

merged_list = list(merge(list_1, list_2))

# print the elements of merged_list object
print(merged_list)

Output from this script:

~]# python3 example-9.py
[1, 2, 3, 4, 5, 6]

 

Summary

In this Python programming tutorial we learned different methods to concatenate multiple lists into single list. You can check the examples and illustrations and decide the method as per your requirement to combine the lists. If you are aware of any further methods, feel free to add via comments section

 

Related Searches: Python concatenate lists, python combine lists, python merge lists, python merge two lists, join two lists python, concatenate two lists python, add two lists python, Python concatenate list of strings. python append list to another list, python combination of two lists

Views: 0

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment