How to concatenate lists in python?

List concatenation is the operation of combining two or more lists. For example, let us consider list_1 = [1, 2, 3] and list_2 = [4, 5]. The concatenation operation of list_1 and list_2 returns the output as [1, 2, 3, 4, 5].

list_1 = [1, 2, 3]
list_2 = [4, 5]
print("list_1= ", list_1)
print("list_2= ", list_2)
list_3 = list_1 + list_2
print("The concatenated list is: ", list_3)

The output of list concatenation

list_1=  [1, 2, 3]
list_2=  [4, 5]
The concatenated list is:  [1, 2, 3, 4, 5]

There are different ways to perform list concatenation. Some of them are as follows.

Concatenation using the '+' operator

The '+' operator is used to join multiple lists together. The argument must be a list. For example, let us consider list_1 = ["apple", "dog", 1, 4.5] and list_2 = ["papaya", 8]. The + operator concatenates two lists and gives output as ['apple', 'dog', 1, 4.5, 'papaya', 8]

list_1 = ["apple", "dog", 1, 4.5]
list_2 = ["papaya", 8]
print("list_1 = : ", list_1)
print("list_2 = : ", list_2)
list_3 = list_1 + list_2
print("The concatenated list is: ", list_3)

The output of list concatenation using + operator

list_1 = :  ['apple', 'dog', 1, 4.5]
list_2 = :  ['papaya', 8]
The concatenated list is:  ['apple', 'dog', 1, 4.5, 'papaya', 8]

Concatenation the list using the for loop and append() method

A for loop is used to traverse the second list and add each item of the second list to the first list by using the append() method. For example, let us consider list_1 = ["apple", "dog", 1, 4.5] and list_2 = ["papaya", 8]. The for loop traverses the list_2 and add the elements of list_2 to list_1 by using append() method.

The list_1 is updated to ['apple', 'dog', 1, 4.5, 'papaya', 8].

list_1 = ["apple", "dog", 1, 4.5]
list_2 = ["papaya", 8]
print("list_1 = : ", list_1)
print("list_2 = : ", list_2)
for item in list_2:
    list_1.append(item)
print("The concatenated list is: ", list_1)

Output for list concatenation using append() method

list_1 = :  ['apple', 'dog', 1, 4.5]
list_2 = :  ['papaya', 8]
The concatenated list is:  ['apple', 'dog', 1, 4.5, 'papaya', 8]

Concatenating the list by using the extend() method

The extend() method is used to add all the items of a sequence to the end of the list. This method takes a single argument and the argument can be tuples, lists, strings, etc. It doesn't return any value. The syntax for the extend() method is as follows.

#syntax:
list.extend(iterable)

For example, let us consider a list_1 = ["apple", "dog", 1, 4.5] .The list_2= ["papaya", "watermelon"] is passed as an argument to extend() function. The list_1 is now updated to ['apple', 'dog', 1, 4.5, 'papaya', 8].

list_1 = ["apple", "dog", 1, 4.5]
list_2 = ["papaya", 8]
print("list_1 = : ", list_1)
print("list_2 = : ", list_2)
list_1.extend(list_2)
print("The concatenated list is: ", list_1)

The output of list Concatenation using extend() method

list_1 = :  ['apple', 'dog', 1, 4.5]
list_2 =:  ['papaya', 8]
The concatenated list is:  ['apple', 'dog', 1, 4.5, 'papaya', 8]

Concatenating the list by using the * operator

The * operator is followed by the list variable unpacking all the elements of the list. This method works only in Python 3.6+. Any number of lists can be concatenated using this method.

Let us consider a list_1 = ["apple", "dog", 1, 4.5] .The list_2= ["papaya", "watermelon"]. The items of both the list_1 and list_2 are unpacked using *list_1 and *list_2.

list_1 = ["apple", "dog", 1, 4.5]
list_2 = ["papaya", 8]
print("list_1 = : ", list_1)
print("list_2 = : ", list_2)
list_3 = [*list_1, *list_2]
print("The concatenated list is: ", list_3)

The output of list concatenation is

list_1 = :  ['apple', 'dog', 1, 4.5]
list_2 = :  ['papaya', 8]
The concatenated list is:  ['apple', 'dog', 1, 4.5, 'papaya', 8]

Concatenating the list by using itertools.chain() method

itertools is an inbuilt module in python having numerous functions which are used to handle iterators. One such itertools function is chain(). The chain() function combines two or more iterators and returns a single iterator.

Let us consider a list_1 = list_1 = ["apple", "dog", 1, 4.5] .The list_2 = ["papaya", "watermelon"]. The itertools.chain(list_1, list_2) combines both the list and returns an iterator.

import itertools
list_1 = ["apple", "dog", 1, 4.5]
list_2 = ["papaya", 8]
print("list_1 = : ", list_1)
print("list_2 = : ", list_2)
list_3 = list(itertools.chain(list_1, list_2))
print("The concatenated list is: ", list_3)

The output for list concatenation using itertools.chain()

list_1 = :  ['apple', 'dog', 1, 4.5]
list_2 = :  ['papaya', 8]
The concatenated list is:  ['apple', 'dog', 1, 4.5, 'papaya', 8]
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions