DEV Community

hrishikesh1990
hrishikesh1990

Posted on

How to concatenate lists in Python?

In this tutorial, we learn how to use Python to concatenate lists. This can be achieved through multiple ways using operators, functions and other methods.

Table of Contents

What is Concatenation of Lists?

Before we look into what concatenation of lists is, let’s first look at what a list is in Python.

A list is one of the most common data structures used, not just in Python but in programming in general. It is an ordered and mutable Python container. To create a list, the elements are placed inside square brackets ([]) and each element is separated by a comma.

With that out of the way, let us look at the various methods that can be used to check if a list is empty in Python.

# list of integers 
number_list = [1, 2, 3, 4, 5]

# example of a list containing different data types
random_list = [1, apple, 4, ball, 7, 9, True]
Enter fullscreen mode Exit fullscreen mode

Joining two or more lists together in Python is called Concatenation.
Now let us look at a few methods of concatenating lists in Python.

Concatenation of lists using the ‘*’ operator in Python

The asterisk ‘*’ operator is useful for many purposes in programming. But one good use for it is using it as an unpacking operator. When used as an unpacking operator, it “unpacks” the contents of a container data structure - such as a list, into another.

As one can see in the code below, using the asterisk operator makes the code short, concise and readable.

Here is an example of how one can use the ‘*’ operator to concatenate two lists, list_one and list_two

Input:

# How to concatenate lists using the ‘*’ operator in Python
list_one = [11, 12, 13]
list_two = [14, 15, 16]
list_three = [*list_one *list_two]
print(list_three)
Enter fullscreen mode Exit fullscreen mode

Output

[11, 12, 13, 14, 15, 16]
Enter fullscreen mode Exit fullscreen mode

One can also concatenate more than two lists easily (example: list4 = [*list_one, *list_two, *list_three])

In terms of performance, the extend() method is several times faster than the asterisk operator. So the concatenation using the asterisk ‘*’ operator may not be the most efficient method when concatenating large lists.

Concatenation of lists using the ‘+’ operator

Another easy way to join lists in Python is to use the ‘+’ operator. The ‘+’ operator is an easy to use method of joining two or more lists together in Python. Its logic and application is simple and easy to understand

When we use the ‘+’ operator on two integers, we’ll get the sum of those integers. But when used on lists, we get a new list which is the concatenation of those lists.

Input:

# Python concatenate lists using the ‘+’ operator in Python
list_one = [11, 12, 13]
list_two = [14, 15, 16]
answer = list_one + list_two
print(The concatenated list is:\n + str(answer))        
Enter fullscreen mode Exit fullscreen mode

Output:

[11, 12, 13, 14, 15, 16]
Enter fullscreen mode Exit fullscreen mode

In this example above, by using the ‘+’ operator, Python has appended list_two at the end of list_one and that has resulted in a new list as output.

The ‘+’ operator, however, creates a new list for each list concatenation operation. This can be a very inefficient manner of using the ‘+’ operator multiple times in a loop.

Concatenation of lists using append() function

As the name suggests, the list_zero.append(x) method appends element x to the end of the list_zero. The append() function method is several times quicker in operation compared to the ‘+’ operator. It is also readable and easy to use.

Here is an example of how we can use the append() method to concatenate two lists, list_one and list_two and store the result in list_one.

Input:

# How to concatenate lists using the append() function in Python
list_one = [9, 8, 7]
list_two = [6, 5, 4]
for element in list_two:
    list_one.append(element)
print(list_one)
Enter fullscreen mode Exit fullscreen mode

Output:

[9, 8, 7, 6, 5, 4]
Enter fullscreen mode Exit fullscreen mode

A major problem with the append() method is that if we want to concatenate two lists we have to iterate over all the elements of the lists and append them one by one. This can translate into an extensive process depending on the size of the lists you are dealing with.

Concatenation of lists using the extend() function

Here is yet another function one can use to join lists together. The difference between the function append() and extend() is that the former adds only one element at a time and the latter adds a collection of elements to the list.

The extend() method is also the most concise and fastest way to concatenate lists compared to all other methods.

Here is an example of how we can use the extend() method to join two lists, list_one and list_two together.

Input:

# How to concatenate lists using the extend() function in Python
list_one = [1, 2, 3]
list_two = [4, 5, 6]
list_one.extend(list_two)
print(list_one)    
Enter fullscreen mode Exit fullscreen mode

Output:

[1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

The method list.extend(iterable) adds all the elements in the iterable to the end of the list.

The extend() method which adds all the elements in the iterable to the end of the list is significantly less complicated compared to the append() method that iterates through each element and appends them one by one.

Closing thoughts

Apart from the methods seen in this tutorial, there are many other ways to concatenate lists in Python. Some examples are the concatenation of lists using the List comprehension method and the Concatenation of lists using the itertools.chain() function. However, the extend() method is said to be the most efficient in terms of performance compared to them all.

Top comments (0)