Python List Concatenation: Best Practices

In Python programming, the most used data structure is a list. It allows us to manipulate and organize data elements efficiently. Lists are particularly useful in data analysis, machine learning, and game development. As a data analyst, we apply different functions using lists. From those concatenation is an important function that involves joining multiple lists into a single.

The process of combining two or more lists into a single unified list is called concatenation. The most common approach to concatenate a list is done by using the “+” operator that appends elements of one list to another.

In this blog, we will explore 9 different concatenation methods that empower data analysts and developers to handle a wide range of programming challenges.

What is a Python List?

Before exploring the concatenation methods, let’s first understand the concept of lists and learn how to create one in Python.

Lists are data structures in Python in which we can store elements with different data types. We can create a list in Python by putting elements inside square brackets as follows.

list = [23, "Absent Data", True, 35.5]
print(list)

Output:

[23, 'Absent Data', True, 35.5]

Nine Ways to Concatenate Lists in Python

The following are Nine ways to concatenate lists in Python. Let’s explore each of them one by one using examples.

  1. Using the “+” operator
  2. Using Naive Method
  3. Using list comprehension
  4. Using extend() method
  5. Using “*” operator
  6. Using itertools.chain()
  7. Merge two List using reduce() function
  8. Using “+=” Operator
  9. Using NumPy `concatenate()` Function

1. Concatenation Using “+” Operator

The most common method to concatenate lists is the use of the “+” operator. It appends the elements of one list at the end of another list. This method is useful only for a small number of lists and not much efficient with a large number of lists due to its memory usage.

Let’s see how we can join two lists using this method:

# Create two lists
list_1 = [1,2,3,4,5]
list_2 = [6,7,8,9,10]

#Cocatenate two list
print("Concatenated List :",list_1 + list_2)

Output:

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

2. Concatenation Using Naive Method

In this method we use for loop to traverse the second list and the elements get appended to the first list. So that the first list would have all the elements.

For example, to merge two lists using the Naive method we will use the following code.

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

for x in list_2 : 
    list_1.append(x) 
print ("Concatenated List :" + str(list_1))

Output:

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

3. Concatenation Using List Comprehension

List concatenating in Python using the comprehension method is a concise way to create a list of elements based on an existing list. It is also used for loop to traverse the list.

We can create a list using the comprehension method by using the following lines of code.

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

list_3 = [j for i in [list_1, list_2] for j in i] 

print ("Concatenated list :"+ str(list_3))

Output:

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

In the above code, the outer loop is for the sub-list, and the inner loop is for elements in the sub-list.

4. Concatenating Using extend() Method

Concatenation can be done by using extend() method in Python. It modifies the original list by adding all the elements of a list to the end of other lists. The syntax to use extend() method is as follows.

list.extend(iterable)

The following code example shows the concatenation using extend() method in Python.

# Initialize two lists
list_1 = [1, 2, 3, 4, 5]
list_2 = [6, 7, 8, 9, 10]

# concatenate list_2 to list_1
list_1.extend(list_2)


print("Concatenated list:", list_1)

Output:

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

Concatenation through extend() method is better than using the “+” operator because it modifies the original list rather than creating a new list.

5. Concatenation Using “*” Operator

In Python asterisk “*” can be used to concatenate two or more lists. The “*” operator unpacks the elements of each list and spreads out them into a new list.

This method is useful to join multiple lists at once. For example, we can join three lists as follows.

# Initialize 3 lists
list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
list_3 = [7, 8, 9]

combined_list = [*list_1, *list_2, *list_3]
print ("Concatenated list :"+ str(combined_list))

Output:

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

6. Concatenation Using itertools.chain()

To concatenate lists we can use itertools.chain() function offered by itertools modules in Python. It is used to concatenate different iterable objects like lists, strings, tuples, and dictionaries in a memory-efficient way.

The following example shows concatenation using itertools.chain() function.

import itertools
list_1 = [1,2,3,4,5]
list_2 = [6,7,8,9,10] 

list_3 = list(itertools.chain(list_1, list_2)) 

print ("Concatenated list :" + str(list_3))

Output:

Concatenated List : [1, 2, 3, 4, 5, 6, 7, 8, 9, 1o]

7. Concatenation Using reduce() Function

In python reduce() function from functools library can be used to concatenate lists but first, we need to import the library.

The following example shows how we can combine two lists using reduce() function.

#import reduce from functools 
from functools import reduce
 
list_1 = [1,2,3,4,5]
list_2 = [6,7,8,9,10]  
 
list_3 = [list_1,list_2]

# using lambda
list_3 = (reduce(lambda i, j: i + j, list_3, []))
print("Concatenated List :", list_3)

Output:

Concatenated List : [1, 2, 3, 4, 5, 6, 7, 8, 9, 1o]

In the above code example we:

  • Initialize two variables that hold two different lists.
  • Create another list to store all the lists taken in the previous step, forming a nested list.
  • Use the reduce() function and pass the nested list as a parameter, along with two variables.
  • Use the lambda function to perform the concatenation operation and store the result in a list.

8. Concatenation Using “+=” Operator

In Python, the augmented assignment (+=) can also be used to concatenate the list just like the extend() method. This operation changes the original list by adding the elements of another list to its end.

The following code shows the concatenation using this approach.

# define two lists
list_1 = [1,2,3,4,5]
list_2 = [6,7,8,9,10] 

# use the '+= operator' to concatenate the lists
list_1 += list_2

# print the result
print("Concatenated list :",list_1)

Output:

Concatenated List : [1, 2, 3, 4, 5, 6, 7, 8, 9, 1o]

The augmented assignment (+=) operator is the best choice to make a new concatenated list when you don’t want to keep the original list because this method modifies the original list.

9. Concatenation Using NumPy concatenate() Function

Python’s NumPy library also offers concatenate() function to join two or more lists but as a result, it returns a NumPy array instead of a list.

The following code can be used to perform concatenation using this method.

import numpy as np

# define two lists
list_1 = [1,2,3,4,5]
list_2 = [6,7,8,9,10] 
result = np.concatenate((list_1, list_2))

print("Concatenated Numpy Array :",result)

Output:

Concatenated Numpy Array : [1, 2, 3, 4, 5, 6, 7, 8, 9, 1o]

We use this method mostly when we are working with numerical data. To use NumPy for concatenation first, we need to import the NumPy library using the following syntax.

import numpy as np

Conclusion

In this article, we have learned the different methods of list concatenation and how we can perform these functions in Python. List concatenation is an important operation in Python and its understanding is necessary for any programmer. It allows us to combine, manipulate, and data transformation in multiple ways. As we know there are many methods of concatenation each has its own strengths and weaknesses in terms of efficiency in handling complex tasks. By mastering different techniques we can choose the best one according to our needs.

Author1

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments