How do i merge two lists in a list python?

All the possible ways to join lists that I could find

import itertools

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

B = [1,3,5,7,9]
B.append([2,4,6,8,10])

C = [1,3,5,7,9]
C.extend([2,4,6,8,10])

D = list(zip([1,3,5,7,9],[2,4,6,8,10]))
E = [1,3,5,7,9]+[2,4,6,8,10]
F = list(set([1,3,5,7,9] + [2,4,6,8,10]))

G = []
for a in itertools.chain([1,3,5,7,9], [2,4,6,8,10]):
    G.append(a)


print("A: " + str(A))
print("B: " + str(B))
print("C: " + str(C))
print("D: " + str(D))
print("E: " + str(E))
print("F: " + str(F))
print("G: " + str(G))

Output

A: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
B: [1, 3, 5, 7, 9, [2, 4, 6, 8, 10]]
C: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
D: [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
E: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
F: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
G: [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]


Join 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.

Example

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:

Example

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:

Example

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 »




In this article we will discuss different ways to Merge / Join two or more lists in python.

Table of Contents

  • Join / Merge two lists in python using + operator.
  • Join / Merge two lists in python using list.extend().
  • Join / Merge two lists in python using unpacking.
  • Join / Merge two lists in python using itertools.
  • Join / Merge two lists in python using for loop.
  • Join / Merge multiple lists in python.

Suppose we have two lists i.e.

# List of strings
list1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list2 = [10, 2, 45, 3, 5, 7, 8, 10]

We want to merge the contents of these two list into a single list i.e.

['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

There are different ways to do this. Let’s discuss them by one by one.

Advertisements

Join / Merge two lists in python using + operator

In python, we can use the + operator to merge the contents of two lists into a new list. For example,

We can use + operator to merge two lists i.e.

# List of strings
list_1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list_2 = [10, 2, 45, 3, 5, 7, 8, 10]

# Merge two lists
final_list = list_1 + list_2

print('Merged List:')
print(final_list)

Output:

Merged List:
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

It returned a new concatenated lists, which contains the contents of both list_1 and list_2. Whereas, list_1 and list_2 remained same as original.

Join / Merge two lists in python using list.extend()

In the previous example, we created a new list containing the contents of the both the lists. But what if we want to extend any existing list? We can extend any existing list by concatenating the contents of any other lists to it using the extend() function of list i.e.

list.extend(anotherList)

list.extend() makes a list longer by appending the elements of another list at the end of the calling list object. For example,

# List of strings
list_1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list_2 = [10, 2, 45, 3, 5, 7, 8, 10]

# Makes list1 longer by appending the elements of list2 at the end.
list_1.extend(list_2)

print('Merged List:')
print(list_1)

Output

Merged List:
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

It extended the list_1 by adding the contents of list_2 at the end of list_1.

Join / Merge two lists in python using unpacking

In python, we can unpack the contents on any iterable object using the * operator. So, *list will unpack the contents of a list. We can unpack the contents of both the lists and create a new list with the merged contents. For example,

# List of strings
list_1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list_2 = [10, 2, 45, 3, 5, 7, 8, 10]

# Merge two lists
final_list = [*list_1, *list_2]

print('Merged List:')
print(final_list)

Output:

Merged List:
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

It unpacked the contents of both the lists and created a new list with the contents of both the lists.

Join / Merge two lists in python using itertools.chain()

In python, the itertools module provides a function chain() to merge the contents of multiple iterable sequences,

itertools.chain(*iterables)

It creates a chain of all the iterable sequences passed as arguments and returns an iterator.

This iterator returns the elements from first iterable sequence until it is exhausted and then moves to the next iterable. We can use this iterator to created a merged list of contents. For example,

import itertools

# List of strings
list_1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list_2 = [10, 2, 45, 3, 5, 7, 8, 10]

# Join two lists
final_list=list(itertools.chain(list_1, list_2))

print('Merged List:')
print(final_list)

Output:

Merged List:
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

Join / Merge two lists in python using for loop

We can iterate over all elements of a list using for loop and during iteration we can append each element to an another list. This way we can extend the contents of a list. For example,

# List of strings
list_1 = ["This" , "is", "a", "sample", "program"]

# List of ints
list_2 = [10, 2, 45, 3, 5, 7, 8, 10]


# Iterate over a list and add elements to another list
for elem in list_2:
    list_1.append(elem)

print('Extended List:')
print(list_1)

Output:

Extended List:
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]

We iterated over all the elements in list_2 and while iteration added each element at the end of list_1. Therefore list_1 is now extended and contains the contents of both the lists i.e. original list_1 and list_2.

Join / Merge multiple lists using + operator

We can merge the contents of multiple lists to a new list using the + operator. For example,

list_1 = ["This" , "is", "a", "sample", "program"]
list_2 = [10, 2, 45, 3, 5, 7, 8, 10]
list_3 = [11, 12, 13]

# Merge 3 lists into a single list    
merged_list = list_1 + list_2 + list_3

print('Merged List:')
print(merged_list)

Output:

Merged List:
['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10, 11, 12, 13]

Conclusion:

We learned about different ways to join or merge multiple lists in python.

How do I merge two lists into a list in Python?

Concatenate Two Lists in Python In almost all simple situations, using list1 + list2 is the way you want to concatenate lists.

How do you mix two lists?

This operation is useful when we have numbers of lists of elements which needs to be processed in a similar manner..
Method #1 : Using Naive Method..
Method #2 : Using + operator..
Method #3 : Using list comprehension..
Method #4 : Using extend().
Method #5 : Using * operator..
Method #6 : Using itertools.chain().

How do I merge two nested lists in python?

First, flatten the nested lists. Take Intersection using filter() and save it to 'lst3'. Now find elements either not in lst1 or in lst2, and save them to 'temp'. Finally, append 'temp' to 'lst3'.