Sling Academy
Home/Python/Ways to Combine Lists in Python

Ways to Combine Lists in Python

Last updated: June 06, 2023

This succinct and practical article walks you through a couple of different ways to combine multiple lists in Python. Each approach will be accompanied by a clear illustrative example. Without any further ado, let’s get started.

Using the + operator

This might be the most straightforward and intuitive method. Other than Python, not many programming languages have a simple syntax like this.

Example:

list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]

combined_list = list1 + list2
print(combined_list)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Another example:

group1 = ["John", "Bob", "Alice"]
group2 = ["Mary", "Jane", "Alice"]
group3 = ["Sling Academy"]

combined_groups = group1 + group2 + group3
print(combined_groups)

Output:

['John', 'Bob', 'Alice', 'Mary', 'Jane', 'Alice', 'Sling Academy']

Using the unpacking operator (*)

Since Python 3.5, you can use the * operator to unpack values from lists and other data structures like dictionaries or tuples (this is quite similar to the spread operator in Javascript and some other programming languages). We can also take advantage of this succinct syntax to join multiple lists together.

Example:

animals = ["dog", "cat", "bird"]
vehicles = ["car", "bike", "plane"]
fruits = ["apple", "banana", "orange"]
flowers = ["rose", "lily", "daisy"]

things = [*animals, *vehicles, *fruits, *flowers]
print(things)

Output:

['dog', 'cat', 'bird', 'car', 'bike', 'plane', 'apple', 'banana', 'orange', 'rose', 'lily', 'daisy']

Using the extend() method

You can use the List.extend() method to concatenate 2 Python lists. It will add all elements from a list to a given list. If you want the original lists intact, you can create a new empty list the call the extend() method on this one. This is exactly what the example below does.

Example:

list1 = ["a", "b" , "c"]
list2 = ["d", "e", "f"]
list3 = ["g", "h", "i"]

joined_list = []
joined_list.extend(list1)
joined_list.extend(list2)
joined_list.extend(list3)

print(joined_list)

Output:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

Using the itertools.chain() function

Another solution to concatenate Python lists.

Example:

import itertools
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]
combined_list = list(itertools.chain(list1, list2, list3))

print(combined_list)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Next Article: Python: 3 Ways to Select Random Elements from a List

Previous Article: Python: Replacing/Updating Elements in a List (with Examples)

Series: Python List Tutorials (Basic and Advanced)

Python

You May Also Like

  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots
  • Monitoring Order Book Imbalances for Trading Signals via cryptofeed
  • Detecting Arbitrage Opportunities Across Exchanges with cryptofeed