7 ways to concatenate two lists in Python

In this Python tutorial, we will understand the implementation of Python Concatenate List.

In Python, we can concatenate lists using the following 7 methods.

  1. Using append()
  2. Using + operator
  3. Using += operator
  4. Using list comprehension
  5. Using extend()
  6. Using * operator
  7. Using itertools.chain()

Python Concatenate List

Let us discuss all 7 methods to concatenate lists in Python. And first, we will discuss the conventional way by using the append() method.

Method 1: Python Concatenate List using append() method

One of the most common ways to concatenate lists in Python is by using the append() method. This method allows adding new elements at the end of the list.

  • So, if we want to concatenate 2 lists, we will use for loop to iterate over each element in the list.
  • In the end, we will use the append() method on the element to add that element to the last of the list.

Here is the approach for this in Python.

# Defining lists
usa_states = ['California', 'Texas', 'Florida']
states = ['Alaska', 'Ohio', 'Hawaii']

# Using append()
 method
for state in states:
    usa_states.append(state)

# Printing the final list
print(usa_states)

In the above example, we first defined 2 lists – usa_states and states respectively.

Then we used the for loop to iterate over each element given in the states list. And we will append that element to the usa_states list using the append() method.

['California', 'Texas', 'Florida', 'Alaska', 'Ohio', 'Hawaii']

Read: Python list methods

Method 2: Python Concatenate List using + operator

Another way to concatenate multiple lists is by using the + operator. Generally, we use the + operator with numeric values to perform mathematical sums. However, the + operator is also used to concatenate strings.

And just like a string, we can also use the + operator with lists to concatenate lists in Python.

Here is an example of this in Python.

# Defining lists
usa_states = ['California', 'Texas', 'Florida']
states = ['Alaska', 'Ohio', 'Hawaii']

# Using + operator
concatenated_list = usa_states + states

# Printing the final list
print(concatenated_list)

In the example, we are concatenating list usa_states with states list using the + operator in Python.

And one we execute the above Python program, we will get the following result.

['California', 'Texas', 'Florida', 'Alaska', 'Ohio', 'Hawaii']

Read: How to Reverse a List in Python

Method 3: Python Concatenate List using += operator

In the previous example, we have seen how to use the + operator to concatenate lists in Python. So, just like we use the + operator, we can also use the addition assignment (+=) operator for this task in Python.

Here is an example of this approach in Python.

# Defining lists
usa_states = ['California', 'Texas', 'Florida']
states = ['Washington', 'Georgia', 'Hawaii']

# Using += operator
usa_states += states

# Printing the final list
print(usa_states)

After executing the above Python program, we will get the following result.

['California', 'Texas', 'Florida', 'Washington', 'Georgia', 'Hawaii']

Read: How to add string to list Python

Method 4: Python Concatenate List using list comprehension

List comprehension in Python is a concise way to create a new list by performing some operation on each item of an existing Python list.

Moreover, we can also use the list comprehension method to concatenate multiple lists into one.

Here is an example of this task in Python.

# Defining lists
usa_cities = ['New York', 'Chicago', 'San Diego']
cities = ['Philadelphia', 'Detroit', 'Atlanta']

# Using list comprehension
concat_list = [city_y for city_x in [usa_cities, cities] for city_y in city_x]


# Printing the final list
print(concat_list)

In the above example, we have taken two different lists in Python. After this, we used list comprehension to concatenate both lists to form one concatenated list.

Here is the final result of the above Python program.

['New York', 'Chicago', 'San Diego', 'Philadelphia', 'Detroit', 'Atlanta']

Read: How to Python Append List to Another List

Method 5: Python Concatenate List using extend()

The extend() method in Python is another way to concatenate multiple lists in Python. However, like the append() method, the extend() method modifies the list in place by adding all the elements of the specified list to the end of the original list.

An example related to the use of extend() method id shown below in Python.

# Defining lists
usa_states = ['California', 'Texas', 'Florida']
states = ['Washington', 'Georgia', 'Hawaii']

# Using extend() method
usa_states.extend(states)

# Printing the final list
print(usa_states)

In the above example, we used the extend() method on the usa_states list to concatenate all the elements of the states list as well.

However, once this Python program is executed, we will get the following result.

['California', 'Texas', 'Florida', 'Washington', 'Georgia', 'Hawaii']

Read: Python program to select from a list

Method 6: Python Concatenate List using * operator

Another way to concatenate lists in Python is by using the * operator within the list. The * operator is generally used in a Python list to unpack all its elements

We can understand the use of the * operator better with the help of the following example.

# Defining lists
usa_cities = ['New York', 'Chicago', 'San Diego']
cities = ['Philadelphia', 'Detroit', 'Atlanta']

# Using * operator in list
concat_list = [*usa_cities, *cities]

# Printing the final list
print(concat_list)

In the above example, we have taken two lists usa_cities and cities respectively. After this, we defined another list and within the square brackets, we defined both lists separately using a comma.

Additionally, to unpack each list, we used the * operator with each list in the square brackets.

After execution, we will get the following result.

['New York', 'Chicago', 'San Diego', 'Philadelphia', 'Detroit', 'Atlanta']

Read: Check if a list exists in another list Python

Method 7: Python Concatenate List using itertools.chain()

The itertools.chain() is a built-in function available under the itertools module. This function can take multiple iterable objects (like list, set, tuple, etc) as input, combines all of them, and return a single iterable object.

Let us see how to use this function in Python to concatenate multiple lists into one.

# Importing itertools
import itertools

# Defining lists
usa_cities = ['New York', 'Chicago', 'San Diego']
cities = ['Philadelphia', 'Detroit', 'Atlanta']

# Using itertools
concat_list = list(itertools.chain(usa_cities, cities))

# Printing the final list
print(concat_list)

In this example, we have combined the usa_cities list with the cities list using itertools.chain(). After this, we converted the single iterable object into a list using the list() function.

The result of the above Python program is shown in the image below.

Python Concatenate List
Concatenating lists in Python using itertools.chain()

You may also like to read the following Python tutorials.

Conclusion

So, in this Python tutorial, we understood how Python concatenate list work. Moreover, we also 7 different methods to concatenate lists in Python. Here is the list of methods that we have covered in this tutorial.

  • Using append()
  • Using + operator
  • Using += operator
  • Using list comprehension
  • Using extend()
  • Using * operator
  • Using itertools.chain()