How does concatenation operator work on list in Python?


In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array.

Concatenation of lists is an operation that adds the elements of one list to the end of another. We're discussing list data structures here, therefore we'll join the elements of one list to the end of another.

In this article, we will show you how to concatenate lists in python. The following are the different methods to accomplish this task −

  • Using Concatenation(+) Operator

  • Using For Loop(Naive Method)

  • Using List Comprehension

  • Using extend() Method

  • Using Unpacking * Operator

  • Using itertools.chain() Method

Assume we have taken a list containing some elements. We will return the concatenated list of the given 2 input lists using the above-specified methods.

Method 1: Using Concatenation(+) Operator

The concatenation operator (+) is the most common way to concatenate lists in Python. As seen in the example below, the "+" operator can easily join the entire list behind another list and return the new list as the resulting output

Example

The following program returns the concatenated list of the given two input lists using the (+) operator −

# input list_1 inputList_1 = [5, 1, 8, 7] # input list_2 inputList_2 = [10, 11, 12] # Concatenating both the lists using the + operator ConcatenatedList = inputList_1 + inputList_2 # Printing the result concatenated list print ("Concatenated list: ", ConcatenatedList)

Output

On executing, the above program will generate the following output −

Concatenated list: [5, 1, 8, 7, 10, 11, 12]

Method 2: Using For Loop(Naive Method)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the first input list.

  • Create another variable to store the second input list.

  • Using the for loop, traverse through each item of the input list_2.

  • Use the append() function( adds the element to the list at the end), to append each item of input list_2 to the inputList_1.

  • Print the result concatenated list

Example

The following program returns the concatenated list of the given two input lists using the Naive method −

# input list_1 inputList_1 = [5, 1, 8, 7] # input list_2 inputList_2 = [10, 11, 12] # Traversing in the input list_2 for item in inputList_2 : # appending each item of input list_2 to the inputList_1 inputList_1.append(item) # Printing the result concatenated list print ("Concatenated list: ", inputList_1)

Output

On executing, the above program will generate the following output −

Concatenated list: [5, 1, 8, 7, 10, 11, 12]

Method 3: Using List Comprehension

The technique of producing list items based on an existing list is known as list comprehension. Using the list comprehensive approach, you can simply concatenate two lists in Python. As seen in the example below, you may use a for loop to traverse the list element and then concatenate it.

Example

The following program returns the concatenated list of the given two input lists using the list comprehension −

# input list_1 inputList_1 = [5, 1, 8, 7] # input list_1 inputList_2 = [10, 11, 12] # Concatenating both lists using list comprehension concatenatedList = [y for x in [inputList_1, inputList_2] for y in x] # Printing the result concatenated list print ("Concatenated list: ", concatenatedList)

Output

On executing, the above program will generate the following output −

Concatenated list: [5, 1, 8, 7, 10, 11, 12]

Method 4: Using extend() Method

To concatenate two lists in Python, we can use the extend() function. The extend() function iterates over the given parameter and adds the item to the list, thus linearly extending the list.

Syntax

list.extend(iterable)

Example

The following program returns the concatenated list of the given two input lists using the extend() method −

# input list_1 inputList_1 = [5, 1, 8, 7] # input list_1 inputList_2 = [10, 11, 12] # Concatenating both the lists using extend() function inputList_1.extend(inputList_2) # Printing the result concatenated list print ("Concatenated list: ", inputList_1)

Output

Concatenated list: [5, 1, 8, 7, 10, 11, 12]

Method 5: Using Unpacking * Operator

The '*' operator in Python can be used to easily concatenate two lists.

In Python, the '*' operator simply unpacks the collection of elements at the index arguments.

Consider the following list: lst = [5, 6, 7, 8]

The statement *my list would replace the list at the index places with its elements. As a result, it unpacks the list of items.

Example

The following program returns the concatenated list of the given two input lists using the unpacking(*) operator −

# input list_1 inputList_1 = [5, 1, 8, 7] # input list_1 inputList_2 = [10, 11, 12] # Concatenating inputList_2 with the inputList_1 using * operator concatenatedList = [*inputList_1, *inputList_2] # Printing the result concatenated list print ("Concatenated list: ", concatenatedList)

Output

Concatenated list: [5, 1, 8, 7, 10, 11, 12]

The line concatenated list = [*inputList_1, *inputList_2] in the preceding code replaces the items in the specified order, i.e. entries of input list1 after elements of input list2. It performs concatenation

Method 6: Using itertools.chain() Method

The itertools.chain() function returns the iterable after chaining its parameters in one and hence does not require storing the concatenated list if only the initial iteration is necessary. This is handy when the concatenated list is only needed once.

Example

The following program returns the concatenated list of the given two input lists using the itertools.chain() function −

import itertools # input list_1 inputList_1 = [5, 1, 8, 7] # input list_1 inputList_2 = [10, 11, 12] # Concatenating inputList_2 with the inputList_1 # using itertools.chain() function concatenatedList = list(itertools.chain(inputList_1, inputList_2)) # Printing the result concatenated list print ("Concatenated list: ", concatenatedList)

Output

Concatenated list: [5, 1, 8, 7, 10, 11, 12]

Conclusion

We learned how to concatenate two lists using six different methods/ways in this article. We also learned how to traverse two lists at the same time using list comprehension. We learned how to concatenate lists using itertools' chain() function

Updated on: 22-Sep-2022

491 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements