How to Concatenate multiple Lists in Python [7 Methods]

In this Python tutorial, I will explain how to concatenate multiple lists in Python using different methods with some illustrative examples. I will also explain what concatenate multiple lists in Python means.

Concatenating multiple lists in Python is a common task when you need to combine the elements of two or more lists into a single list. In this article, we will explore different approaches to concatenate multiple lists in Python in detail.

Methods to concatenate multiple lists in Python

There are many different ways present in Python to concatenate multiple lists in Python.

  • Using the + Operator
  • Using += Operator
    • Using += with Slicing
  • Using extend() Method
  • Using append() and a for loop
  • Using List Comprehension
  • Using * Operator (Python 3.5+)
  • Using itertools.chain()

Let’s see them one by one using some demonstrative examples:

Method 1: Python join multiple lists using the + operator

The + operator allows us to concatenate two or more lists by creating a new list containing all the elements from the input lists in Python.

Example: Imagine we have two Python lists and I want to create a single list through Python.

east_coast_cities = ["New York", "Boston", "Washington, D.C."]
west_coast_cities = ["Los Angeles", "San Francisco", "Seattle"]

all_major_cities = east_coast_cities + west_coast_cities
print(all_major_cities)

Output: The + operator combines the above two lists in Python to create a new list as you can see below

['New York', 'Boston', 'Washington, D.C.', 'Los Angeles', 'San Francisco', 'Seattle']
python join multiple lists

This way we can use the + operator to concatenate multiple lists in Python.

Method 2: Python concatenate multiple lists using the += operator

The += operator in Python can be used to concatenate multiple lists in place without creating a new list. It extends the first Python list with elements from the second Python list.

READ:  How to add a string to a list in Python

We can also use slicing with the += operator to concatenate multiple lists in Python.

Scenario 1: We are developing a Python program for tracking expenses in different categories. Initially, we have a list of expenses and we want to add the expenses for food to that list too.

accommodation_expenses = [150, 200, 180]
food_expenses = [50, 60, 70]

accommodation_expenses += food_expenses
print(accommodation_expenses)

Output: The += operator modifies the list of expenses in place by adding the elements from another Python list.

[150, 200, 180, 50, 60, 70]
python concatenate multiple lists

Scenario 2: Consider a situation, where we have separate lists of attractions. To create a single Python list that includes all the attractions, we want to use slicing and the += operator.

east_coast_attractions = ["Statue of Liberty", "Washington Monument", "Walt Disney World"]
west_coast_attractions = ["Golden Gate Bridge", "Hollywood Walk of Fame", "Grand Canyon"]

all_attractions = []
all_attractions[len(all_attractions):] = east_coast_attractions
all_attractions[len(all_attractions):] = west_coast_attractions
print(all_attractions)

Output: In this scenario, we initialize an empty Python list and then use slicing with the += operator to add the attractions from both lists to the same empty list in Python.

The [len(all_attractions):] is a slice that essentially means “from the current end of the list to the end of the list,” which is an empty slice.

['Statue of Liberty', 'Washington Monument', 'Walt Disney World', 'Golden Gate Bridge', 'Hollywood Walk of Fame', 'Grand Canyon']
concatenate multiple lists python

This way we can use the += operator alone or slicing to concatenate multiple lists in Python.

Method 3: Python extend multiple lists using extend() method

The extend() method is used to concatenate Python lists in place. It appends all the elements from one list to another, modifying the first list in Python without creating a new one.

Example: We are building a Python program that tracks populous states. We start with a Python list of some states and then want to add two more from another Python list which are added later.

top_states = ["California", "Texas", "Florida"]
additional_states = ["New York", "Pennsylvania"]

top_states.extend(additional_states)
print(top_states)

Output: The extend() method modifies the first Python list in place by adding the elements from another list in Python.

['California', 'Texas', 'Florida', 'New York', 'Pennsylvania']
python concat multiple lists

This way we can join multiple lists in Python using the extend() function.

READ:  How to concatenate tuples in Python [9 different ways]

Method 4: How to concatenate multiple lists in Python using append() with for loop

The append() method in Python is used to add an element to the end of a list in Python. When combined with a for loop, it can be a powerful tool to concatenate multiple lists in Python into one.

Example: Let’s consider a scenario where we have multiple Python lists, and we want to create a single list of all using the append() method and a for loop:

east_coast_states = ["New York", "New Jersey", "Connecticut", "Massachusetts"]
west_coast_states = ["California", "Oregon", "Washington"]
all_us_states = []
for state in east_coast_states:
    all_us_states.append(state)

for state in west_coast_states:
    all_us_states.append(state)

print(all_us_states)

Output: By iterating over each element present in the Python list. We add them to our empty list and then at last, we get a list in Python that contains all the elements from every list in Python.

['New York', 'New Jersey', 'Connecticut', 'Massachusetts', 'California', 'Oregon', 'Washington']
append multiple lists python

This wee can concatenate multiple lists in Python using the append() function in the for loop.

Method 5: Concatenate three lists Python using list comprehension

The list comprehension is a versatile technique for creating new lists in Python by iterating through one or more existing Python lists. We can use it to concatenate multiple lists by creating a new list with elements from the original lists through Python.

Example: We have various data stored in separate lists in Python, and we want to combine them into a single Python list.

california_population = [39538223]
texas_population = [29145505]
new_york_population = [20330589]

state_populations = [california_population, texas_population, new_york_population]
us_population = [population for state_population in state_populations for population in state_population]
print(us_population)

Output: Here, the list comprehension is used to iterate through the list in Python, which contains the individual Python lists, and concatenate them into a single list in the last.

[39538223, 29145505, 20330589]
python combine three lists

This way we can use the list comprehension to concatenate multiple lists in Python.

Method 6: Python combine multiple lists using the * operator

The * operator is used to unpack and concatenate multiple lists in Python. This operator allows us to create a new Python list by unpacking elements from the input lists.

READ:  Key Index in Python Dictionary

Example: We have different sales data stored in separate lists in Python, and we want to combine them into a single list to analyze the overall sales through Python.

west_coast_sales = [35000, 42000, 38000, 41000]
midwest_sales = [28000, 32000, 29000, 31000]
east_coast_sales = [40000, 45000, 39000, 42000]
all_sales_data = [*west_coast_sales, *midwest_sales, *east_coast_sales]
print(all_sales_data)

Output: In this example, the * operator is used to unpack the elements of each sales data Python list and concatenate them into the list in Python.

[35000, 42000, 38000, 41000, 28000, 32000, 29000, 31000, 40000, 45000, 39000, 42000]
concat multiple lists python

This way we can unpack the elements using the * operator in Python to concatenate multiple lists.

Method 7: Combine multiple lists Python using itertools.chain()

The itertools.chain() function from the itertools module efficiently concatenate multiple lists in Python by returning an iterator that yields elements from each input iterable. It does not create a new list in Python.

Example: We have separate lists in Python of area codes, and we want to create a single list of area codes to use in our Python program.

from itertools import chain

area_codes_state1 = ["212", "646"]
area_codes_state2 = ["310", "213"]

all_area_codes = list(chain(area_codes_state1, area_codes_state2))
print(all_area_codes)

Output: We use the chain function to concatenate lists in Python. The chain function takes multiple iterable arguments and returns an iterator that produces elements from those iterables. To convert the iterator into a list, we wrap it with the list() constructor.

['212', '646', '310', '213']
Concatenate multiple lists in Python

This way we simply use the chain() function in the itertools module to concatenate multiple lists in Python.

Conclusion

Understanding how we can concatenate multiple lists in Python can be accomplished using several methods like using the + Operator, append() with a for loop, extend() Method, List Comprehension, the * Operator, itertools.chain(), and the += Operator alone or with slicing, each with its advantages and use cases that are explained with the help of some illustrative examples.

The choice of method depends on our specific requirements and coding style.

You may also like to read: