How to append list to second list (concatenate lists) 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.

In this article, we are going to append the list to another list(Concatenate Lists) in Python. The following are the different methods to accomplish this task −

  • Using Concatenation(+) Operator

  • Using the append method of the list object

  • Using extend() Method

  • Using itertools.chain() Method

  • Obtain a list without duplicates

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

Add the second list to the first list using the concatenation operator(+) and stored it in the first list.

Example

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

# first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code"] # Adding the second list(Concatenating) to the first list givenList1 = givenList1 + givenList2 # Printing the first List after concatenating it with the second list print ("First list after concatenating with the second list: ", givenList1)

Output

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

First list after concatenating with second list: ['Hello', 10, 'TutorialsPoint', 20, 'python', 'code']

Method 2: Using the append() method of the list object

Algorithm (Steps)

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

  • Create a variable to store the first input list and initialize it with some random values. Similarly, Create another list (second input list) with another set of random values.

  • Append the second input list to the first input list by passing the second input list as an argument to the append() function(adds the element to the list at the end).

  • Print the first input list after concatenating it with the second input list.

Example

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

# first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code"] # Adding the second list(Concatenating) to the first list using the append() function givenList1.append(givenList2) # Printing the first List after concatenating it with the second list print ("First list after concatenating with the second list: ", givenList1)

Output

On executing, the above program will generate the following output

First list after concatenating with second list: ['Hello', 10, 'TutorialsPoint', 20, ['python', 'code']]

Method 3: 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 −

# first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code"] # Adding the second list(Concatenating) to the first list using the extend() function givenList1.extend(givenList2) # Printing the first List after concatenating it with the second list print ("First list after concatenating with the second list: ", givenList1)

Output

First list after concatenating with second list: ['Hello', 10, 'TutorialsPoint', 20, 'python', 'code']

Method 4: 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 # first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code"] # Adding the second list(Concatenating) to the first list using itertools.chain() function givenList1 = list(itertools.chain(givenList1, givenList2)) # Printing the first List after concatenating it with the second list print ("First list after concatenating with the second list: ", givenList1)

Output

First list after concatenating with second list: ['Hello', 10, 'TutorialsPoint', 20, 'python', 'code']

Method 5: Obtain a list without duplicates

Algorithm (Steps)

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

  • Create a variable to store the first input list and initialize it with some random values. Similarly, Create another list (second input list) with another set of random values.

  • Add the second list to the first list using the concatenation operator(+) and stored it in the first list.

  • Convert the first list to a set using the set() function (It removes duplicates) and convert this set back to a list using the list() function.

  • Print the result list i.e list without duplicates.

Example

The following program returns the concatenated list of the given two input lists without duplicates −

# first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code", 20, "TutorialsPoint"] # Adding the second list(Concatenating) to the first list givenList1 = givenList1 + givenList2 # Removing duplicates from the concatenated list uniqueList=list(set(givenList1)) # Printing the concatenated list after removing duplicates print ("Concatenated list after removing duplicates: ", uniqueList)

Output

Concatenated list after removing duplicates: ['TutorialsPoint', 10, 'Hello', 'python', 20, 'code']

Conclusion

We learned how to append a list to another list (list concatenation) using four different methods, including the concatenation Operator (+), append(),extend(), and chain() functions. We also learned how to remove duplicates from a concatenated list.

Updated on: 19-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements