Python Join Lists [6 Methods with Examples]

 

Introduction to Python join lists

The list is one of the data structures in Python that can store different data typed elements. In this tutorial, we will learn how we can join two lists together in python. We will cover various different ways to join lists together along with examples. We will cover the Naive method, using + operator, using * operator, list comprehension, itertools.chain() method, and the extend() method to join multiple lists together. In a nutshell, this tutorial will contain all the necessary methods that you need to know in order to join lists in Python.

 

Getting started with Python join lists

A list is an ordered and mutable Python container, being one of the most common data structures in Python. To create a list, the elements are placed inside square brackets ([]), separated by commas. A list can contain different data typed elements. For example, see the list of numeric values below:

Advertisement
# creating list
mylist = [1, 2, 3, 4, 5, 6]
# printing
print(mylist)

Output:

[1, 2, 3, 4, 5, 6]

It can also contain tuples, strings, or dictionaries. See the example below:

# creating list
mylist = ["Golinuxcloud", (1, 2, 3, 4), {"key": "value"}]
# printing
print(mylist)

Output:

['Golinuxcloud', (1, 2, 3, 4), {'key': 'value'}]

Notice that the above list has different data typed elements. A list can also contain a list as well. See the example below:

# creating list
mylist = [[1, 2, 3, 4, 5], [2, 3, 4, 5]]
# printing
print(mylist)

Output:

[[1, 2, 3, 4, 5], [2, 3, 4, 5]]

Such a list that contains lists is called a multidimensional or nested list. In the upcoming sections, we will discuss some of the useful ways to join lists together.

Advertisement

 

Method-1: Python join lists using + operator

Usually, the plus (+) operator is used for the arithmetic addition in python but it can also be used to concatenate strings or lists. When we use the plus (+) operator between two lists, it appends one list at the end of the other list and results in a new list as output. But if we will put the plus (+) operator in between any numeric values, it will add them together.

 

Examples of Python join lists using + operator

Now let us take an example and see how the plus (+) operator joins two lists together. See the example below:

# creating list
mylist = [1, 2, 3, 4]
mylist1 = [4, 3, 5 ,32]
# python join lists
result = mylist + mylist1
# printing
print(result)

Output:

[1, 2, 3, 4, 4, 3, 5, 32]

Notice that the plus (+) operator had added one list to the end of another list. It can also be used to add more than two lists together. See the example below:

# creating lists
mylist1 = ["Welcome"]
mylist2 = ["go"]
mylist3 = ["linux"]
mylist4 = ["cloud"]
# Python join lists
result = mylist1 + mylist2 + mylist3 + mylist4
# printing 
print(result)

Output:

['Welcome', 'go', 'linux', 'cloud']

Notice that the plus (+) operator added all the lists to the end of each other and returned a new list.

 

Method-2: Python join lists using * operator

Usually, the asterisk sign (*) operator is used for the arithmetic multiplication in python but it can also be used to concatenate lists. All we need to do is to create new lists containing the names of lists that we want to join along with the asterisk sign(*).  The ‘*’ operator in Python basically unpacks the collection of items at the index arguments. If we will just put the asterisk sign(*) in between the two lists as we did for the plus (+) operator, we will get an error.

 

Examples of Python joins lists using * operator

Now let us take an example and join two lists together using the asterisk sign. See the example below:

Advertisement
# creating lists
mylist1 = [1, 2, 3, 4, 5]
mylist2 = [6, 7, 8, 9, 0]
# python join lists
result = [*mylist1, *mylist2]
# printing
print(result)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

In the above snippet of code, the statement [*mylist1, *mylist2] replaces lmyist1 and mylist2 with the items in the given order i.e. elements of mylist2 after elements of the mylist1. In a similar way, we can join multiple lists together using the same method. See the example below:

# creating lists
mylist1 = ["Welcome"]
mylist2 = ["go"]
mylist3 = ["linux"]
mylist4 = ["cloud"]
# Python join lists
result = [*mylist1, *mylist2, *mylist3, *mylist4]
# printing 
print(result)

Output:

['Welcome', 'go', 'linux', 'cloud']

Notice that we have successfully joined multiple lists together using the '*' operator.

 

Method-3: Python join lists using Naive method

A for Loop in Python is used to repeat a specific block of code a known number of times. You can read more about the syntax and function of for loop from the article Python for loop. Here we will use python to loop in the Naive method to join lists together. In the Naive method, a for loop is used to traverse the second list. After this, the elements from the second list get appended to the first list. The first list results out to be the concatenation of the first and the second list. It is simply iterating the second list and appending the elements to the first list. 

 

Example of Python join lists using Naive method

We already discussed how we can join lists using the naive method. Let us take an example and see how we can append the elements from one list to another using this method. See the example below:

# creating lists
mylist1 = [1, 2, 3, 4, 5]
mylist2 = [6, 7, 8, 9, 0]
# python join lists using naive method
for i in mylist2:
    mylist1.append(i)
# printing
print(mylist1)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

In the above example, the for loop iterates through all the elements of the second list and appends them to the first list. The advantage of this method is that it does not create a new list, it just appends elements from one list to another list. But it is not a useful method to join more than two lists because we have to create a for loop for each of the lists. For example, see the python program below:

Advertisement
# creating lists
mylist1 = ["Welcome"]
mylist2 = ["go"]
mylist3 = ["linux"]
mylist4 = ["cloud"]
# Python join lists using naive method
for i in mylist2:
    mylist1.append(i)
for i in mylist3:
    mylist1.append(i)
for i in mylist4:
    mylist1.append(i)
# printing 
print(mylist1)

Output:

['Welcome', 'go', 'linux', 'cloud']

Notice that we had created a for loop for each list to iterate through the elements and append them to the first list.

 

Method-4: Python join lists using list comprehension

List comprehension is a pythonic way of expressing a for loop that appends to a list in a single line of code. It is much faster than for loop and map() function. You can read more about list comprehension in python from the article on Python list comprehension. We can use list comprehension to join lists together. Again we will use for loop inside to iterate over the elements of the lists.

 

Examples of Python join lists using list comprehension

Python list comprehension is an alternative method to concatenate two lists in Python. List Comprehension is basically the process of building/generating a list of elements based on an existing list. Let us take an example and see how it actually works. See the example below:

# creating lists
mylist1 = [1, 2, 3, 4, 5]
mylist2 = [6, 7, 8, 9, 0]
# python join lists using list comprehension
result = [j for i in [mylist1, mylist2] for j in i] 
# printing
print(result)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

The list comprehension in the above program works like a nested list. It iterates through the elements of both lists and then creates a new list containing all the elements of the given lists. We can use the same method to iterate over multiple lists and join them together. See the example below:

# creating lists
mylist1 = ["Welcome"]
mylist2 = ["go"]
mylist3 = ["linux"]
mylist4 = ["cloud"]
# Python join lists using list comprehension
result = [j for i in [mylist1, mylist2, mylist3, mylist4] for j in i] 
# printing 
print(result)

Output:

['Welcome', 'go', 'linux', 'cloud']

Notice that the for loop first iterates through the list of lists and then through each element in each list and creates a new list containing all the elements.

 

Method-5: Python join lists using extend() method

The extend() is a Python built-in function that extends the list by appending all the items from the iterable. It takes iterable as an argument and appends all the elements to the list. In this section, we will take examples and will see how it actually works.

 

Example of Python join lists using extend() method

Let us take an example and join two lists together using extend() method. See the example below:

# creating lists
mylist1 = [1, 2, 3, 4, 5]
mylist2 = [6, 7, 8, 9, 0]
# python join lists using extend() method
mylist1.extend(mylist2)
# printing
print(mylist1)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Notice that the extend() method does not create a new list, it just adds the elements from one list to another list. But we can not use this method to join more than two lists as it can take only one parameter. But we can use list comprehension to join more than two lists. See the example below:

# creating lists
mylist1 = ["Welcome"]
mylist2 = ["go"]
mylist3 = ["linux"]
mylist4 = ["cloud"]
# Python join lists using extend method
mylist1.extend([j for i in [mylist2, mylist3, mylist4] for j in i])
# printing 
print(mylist1)

Output:

['Welcome', 'go', 'linux', 'cloud']

Notice that the list comprehension in the above example will iterate through the given lists and return one list containing all the given lists and then the extend method appends those elements to the specified list.

 

Method-6: Python join lists using itertools.chain() method

The chain() method takes iterables as input and returns a chain object. In other words, this method can take one or more than one optional iterables, combines them as one iterable, and returns each element in sequential order. If no arguments are passed, then it returns an object which has no elements. In this section, we will use the chain() method to join multiple lists together.

 

Examples of Python join lists using itertools.chain() method

First, we have to import itertools library in order to use the chain() method. If it is not installed on your system, you can use pip command to install itertools. Once the library is installed, we are ready to use the chain() method to join lists together. See the example below which joins two lists together using chain() method.

# importing intertools
import itertools
# creating lists
mylist1 = [1, 2, 3, 4, 5]
mylist2 = [6, 7, 8, 9, 0]
# python join lists using chain() method
result = list(itertools.chain(mylist1, mylist2)) 
# printing
print(result)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Notice that the chain method had joined the two lists together and return a new list containing all the elements from the two lists. In a similar way, we can use the same method to join more than two lists. See the example below:

# importing intertools
import itertools
# creating lists
mylist1 = ["Welcome"]
mylist2 = ["go"]
mylist3 = ["linux"]
mylist4 = ["cloud"]
# python join lists using chain() method
result = list(itertools.chain(mylist1, mylist2, mylist3, mylist4)) 
# printing 
print(result)

Output:

['Welcome', 'go', 'linux', 'cloud']

Notice that the chain() method can take multiple arguments and return a list in the appended form.

 

Summary

In this article, we learned about joining multiple lists together to create one list. We covered six different ways to concatenate lists together through various examples. We discussed + operator, * operator, chain() method, extend() method, the naive method, and list comprehension to join different lists together. To summarize, this article covers all the necessary methods that you need to know in order to start concatenating lists together.

 

Further Reading

Python lists
Python data structures
Python lists methods

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

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 either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment