Write a lambda function to concatenate two lists

There are several ways to join, or concatenate, two or more lists in Python.

One of the easiest ways are by using the + operator.

Join two list:

list1 = ["a", "b" , "c"]list2 = [1, 2, 3]list3 = list1 + list2

print(list3)

Try it Yourself »

Another way to join two lists are by appending all the items from list2 into list1, one by one:

Append list2 into list1:

list1 = ["a", "b" , "c"]list2 = [1, 2, 3] for x in list2:  list1.append(x)

print(list1)

Try it Yourself »

Or you can use the extend() method, which purpose is to add elements from one list to another list:

Use the extend() method to add list2 at the end of list1:

list1 = ["a", "b" , "c"]list2 = [1, 2, 3]list1.extend(list2)

print(list1)

Try it Yourself »


List concatenation the act of creating a single list from multiple smaller lists by daisy chaining them together.

There are many ways of concatenating lists in Python. Specifically, in this article, we'll be going over how to concatenate two lists in Python using the plus operator, unpack operator, multiply operator, manual for loop concatenation, the itertools.chain() function and the inbuilt list method extend().

In all the code snippets below, we'll make use of the following lists:

list_a = [1, 2, 3, 4] list_b = [5, 6, 7, 8]

Plus Operator List Concatenation

The simplest and most straightforward way to concatenate two lists in Python is the plus (+) operator:

list_c = list_a + list_b print (list_c) [1, 2, 3, 4, 5, 6, 7, 8]

Unpack Operator List Concatenation

This method allows you to join multiple lists. It is a fairly new feature and only available from Python 3.6+. The unpacking operator, as the name implies, unpacks an iterable object into its elements. Unpacking is useful when we want to generate a plethora of arguments from a single list. For example:

def foo(a, b, c, d): return a + b + c + d foo(*list_a) 10

In a nutshell, we use the list constructor ([a,b..]) and generate the elements of the new list in order by unpacking multiple lists one after another:

list_c = [*list_a, *list_b, *list_a] print (list_c) [1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4]

Multiply Operator List Concatenation

The multiply (*) operator is special case of list concatenation in Python. It is used to repeat a whole list multiple times (which is why it's denoted with a multiplication operator):

print(list_a * 2) [1, 2, 3, 4, 1, 2, 3, 4]

for loop List Concatenation

In this method we will go through one list while appending each of its elements to another list one by one. When the loop is over you will have a single list with all the desired elements:

for i in list_b: list_a.append(i) print (list_a) [1, 2, 3, 4, 5, 6, 7, 8]

This method works with iterables. It constructs and returns an iterator that can later be used to construct the chained list (think of it as an arrow which just memorizes the order of elements in the resulting list):

iterator = itertools.chain([1, 2], [3, 4]) list(iterator)

Under the hood, something along these lines is what happens:

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

[1, 2], [3, 4] ^ [1, 2], [3, 4] ^ [1, 2], [3, 4] ^ [1, 2], [3, 4] ^ list([1,2,3,4])

For this method, you will need to import itertools:

import itertools list_c = list(itertools.chain(list_a, list_b)) print (list_c) [1, 2, 3, 4, 5, 6, 7, 8]

extend() List Concatenation

This is aa built-it function that can be used to expand a list. Here we are expanding the first list by adding elements of the second list to it:

list_a.extend(list_b) print (list_a) [1, 2, 3, 4, 5, 6, 7, 8]

Conclusion

In this article, we've gone over five ways to concatenate two lists in Python - using the plus operator, the unpack operator, the multiply operator, a for loop, itertools.chain() and extend().

In this tutorial, we will unveil different methods to concatenate lists in Python. Python Lists serve the purpose of storing homogeneous elements and perform manipulations on the same.

In general, Concatenation is the process of joining the elements of a particular data-structure in an end-to-end manner.

The following are the 6 ways to concatenate lists in Python.

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

1. Concatenation operator (+) for List Concatenation

The '+' operator can be used to concatenate two lists. It appends one list at the end of the other list and results in a new list as output.

Example:

list1 = [10, 11, 12, 13, 14] list2 = [20, 30, 42] res = list1 + list2 print ("Concatenated list:\n" + str(res))

Output:

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

2. Naive Method for List Concatenation

In the Naive method, a for loop is used to traverse the second list. After this, the elements from the second list get appended to the first list. The first list results out to be the concatenation of the first and the second list.

Example:

list1 = [10, 11, 12, 13, 14] list2 = [20, 30, 42] print("List1 before Concatenation:\n" + str(list1)) for x in list2 : list1.append(x) print ("Concatenated list i.e. list1 after concatenation:\n" + str(list1))

Output:

List1 before Concatenation: [10, 11, 12, 13, 14] Concatenated list i.e. list1 after concatenation: [10, 11, 12, 13, 14, 20, 30, 42]

3. List Comprehension to concatenate lists

Python List Comprehension is an alternative method to concatenate two lists in Python. List Comprehension is basically the process of building/generating a list of elements based on an existing list.

It uses for loop to process and traverses the list in an element-wise fashion. The below inline for-loop is equivalent to a nested for loop.

Example:

list1 = [10, 11, 12, 13, 14] list2 = [20, 30, 42] res = [j for i in [list1, list2] for j in i] print ("Concatenated list:\n"+ str(res))

Output:

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

4.Python extend() method for List Concatenation

Python’s extend() method can be used to concatenate two lists in Python. The extend() function does iterate over the passed parameter and adds the item to the list thus, extending the list in a linear fashion.

Syntax:

list.extend(iterable)

Example:

list1 = [10, 11, 12, 13, 14] list2 = [20, 30, 42] print("list1 before concatenation:\n" + str(list1)) list1.extend(list2) print ("Concatenated list i.e ,ist1 after concatenation:\n"+ str(list1))

All the elements of the list2 get appended to list1 and thus the list1 gets updated and results as output.

Output:

list1 before concatenation: [10, 11, 12, 13, 14] Concatenated list i.e ,ist1 after concatenation: [10, 11, 12, 13, 14, 20, 30, 42]

5. Python ‘*’ operator for List Concatenation

Python’s '*' operator can be used to easily concatenate two lists in Python.

The ‘*’ operator in Python basically unpacks the collection of items at the index arguments.

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

The statement *my_list would replace the list with its elements at the index positions. Thus, it unpacks the items of the lists.

Example:

list1 = [10, 11, 12, 13, 14] list2 = [20, 30, 42] res = [*list1, *list2] print ("Concatenated list:\n " + str(res))

In the above snippet of code, the statement res = [*list1, *list2] replaces the list1 and list2 with the items in the given order i.e. elements of list1 after elements of list2. This performs concatenation and results in the below output.

Output:

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

Python itertools modules’ itertools.chain() function can also be used to concatenate lists in Python.

The itertools.chain() function accepts different iterables such as lists, string, tuples, etc as parameters and gives a sequence of them as output.

It results out to be a linear sequence. The data type of the elements doesn’t affect the functioning of the chain() method.

For example: The statement itertools.chain([1, 2], [‘John’, ‘Bunny’]) would produce the following output: 1 2 John Bunny

Example:

import itertools list1 = [10, 11, 12, 13, 14] list2 = [20, 30, 42] res = list(itertools.chain(list1, list2)) print ("Concatenated list:\n " + str(res))

Output:

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

Conclusion

Thus, in this article, we have understood and implemented different ways of Concatenating lists in Python.