Easy Ways for Python List Concatenate with Example Codes

Python List Concatenate

Python lists are used to store items that are all the same and to change those same items.

Meanwhile, Python List Concatenation is the process of merging all of the parts of a data structure together from beginning to end.

Here are the six ways to concatenate a list in Python.

  • Naive Method
  • concatenation (+) operator
  • List Comprehension
  • extend() method
  • *’ operator
  • itertools.chain() method

This operation is useful when we have a number of lists of elements that need to be processed in a similar manner.

Using the Naive Method Python

The Naive method is we keep adding elements from the second list to the first list as we go through the second list.

This way, the first list will have all of the elements from both lists so that the append will be done.

Here’s an example of Naive Method:

list1 = [10, 11, 12, 13, 14] 
list2 = [15, 30, 40] 

print("List1 before Concatenation:\n" + str(list1))
for x in list2 : 
    list1.append(x) 


print ("Concatenated list using naive method :\n" + str(list1))

Output:

List1 before Concatenation:
[10, 11, 12, 13, 14]
Concatenated list using naive method :
[10, 11, 12, 13, 14, 15, 30, 40]

Using Concatenation operator (+) in Python

The '+' operator is used to concatenate two lists. It adds one list to the end of another list, creating a new list as a result.

This is the most practical way to perform list concatenation. 

Example of Concatenation using operator:

list1 = [20, 21, 22, 23, 24] 
list2 = [25, 30, 42] 


res = list1 + list2 


print ("Concatenated list using  operator (+):\n" + str(res))

Output:

Concatenated list using operator (+):
[20, 21, 22, 23, 24, 25, 30, 42]

Using List Comprehension Python

Python’s List Comprehension can also concatenate lists.

It is an alternative method to concatenate two lists in Python.

List comprehension is essential for the generation of elements of the list from an existing list.

It processes using a for loop and iteratively traverses the list’s elements.

The subsequent inline for loop is comparable to nested for loops.

Example of List Comprehension:

list1 = [10, 11, 12, 13, 14] 
list2 = [20, 30, 42] 

res = [j for i in [list1, list2] for j in i] 

print ("Concatenated list Using List Comprehension:\n"+ str(res))

Output:

Concatenated list Using List Comprehension:
 [10, 11, 12, 13, 14, 20, 30, 42]

Using extend method python

The extend() method in Python can be used to concatenate two lists.

The extend() function iterates over the provided parameter and adds the item to the list, hence linearly increasing the list.

Syntax:

list.extend(iterable)

Example:

list1 = [11, 12,13, 14, 15] 
list2 = [21, 22, 24] 
print("list1 before concatenation:\n" + str(list1))
list1.extend(list2) 
print ("List1 after concatenation Using extend method:\n"+ str(list1))

All of the elements of list2 are combined to list1, so updating list1 and generating output below.

Output:

list1 before concatenation:
[11, 12, 13, 14, 15]
List1 after concatenation Using extend method:
[11, 12, 13, 14, 15, 21, 22, 24]

Using Python ‘*’ operator

Python’s '*' operator can be used to concatenate two lists in Python easily. Python’s ‘*‘ operator basically unpacks the collection of objects specified by the index parameters.

This feature was introduced in version 3.6. Using the * operator allows us to concatenate multiple lists into one list.

For instance[*list1, *list2] – concatenates the items in list1 and list2, and new list is created.

For example: Consider a list my_list = [1, 2, 3, 4].

The statement *my_list would replace the list’s elements at their respective index locations.

Therefore, it unpacks the list of items.

Example program:

list1 = [10, 11, 12, 13, 14] 
list2 = [20, 30, 42] 

res = [*list1, *list2] 
  
print ("Concatenated list Using Python ‘*’ operator:\n " + str(res))

Output:

Concatenated list Using Python ‘*’ operator:
 [10, 11, 12, 13, 14, 20, 30, 42]

Using Yython itertools chain

itertools module possesses the in-built function “chain()” which helps simply combining lists.

The itertools.chain() method accepts multiple data structures and results in the linear sequence as the output.

The itertools.chain() method takes as input different types of iterables, such as lists, strings, tuples, etc., and returns a chain of them.

It concludes in a linear sequence that the data type of the items has no effect on how the chain() method operates.

It is important to keep in mind that the data type of the element does not have any effect on how the chain() method operates.

Additionally, you need to import the itertools library through the “import” keyword, as seen in the example that follows:

Example of list itertools chain:

import itertools
list1 = [10, 11, 12, 13, 14] 
list2 = [20, 30, 42] 

res = list(itertools.chain(list1, list2)) 
   
  
print ("Concatenated list Using python itertools chain:\n " + str(res))

Output:

Concatenated list Using python itertools chain:
 [10, 11, 12, 13, 14, 20, 30, 42]

Conclusion

To summarize, we’ve learned and understood how to implement different ways of Python List Concatenate.

We know that lists are the most common data format in programming, and we frequently encounter the need to combine two or more lists.

Six easy ways for concatenating lists in Python with complete examples and output are already given above.

It is highly suggested that you learn and comprehend this technique for efficient programming.

Aside from this topic you can also read: Lowercase Python String with Example Programs

Leave a Comment