Search code snippets, questions, articles...

Join two or multiple lists in Python

Here, we are showing you the different methods that can be used to join or concatenate two or multiple lists in Python.
list_1 = [1, 2]
list_2 = [3, 4]

result = list_1 + list_2

print(result)
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

The simplest method to join two lists is by using the + operator and placing the + keyword between lists that you want to concatenate.

In the above code snippet:

  1. We have defined two lists - list_1 and list_2
  2. We are joining them using the + operator and assigning them to the result variable that will contain the merged list.
  3. Print the final list - result.

Join three lists using + operator

list_1 = ['Apple', 'Banana']
list_2 = ['Orange']
list_3 = ['papaya']

result = list_1 + list_2 + list_3

print(result)

Output

['Apple', 'Banana', 'Orange', 'papaya']

Join two lists using extend() function

You can join two lists using extend() function too. The basic syntax to join two lists using extend() function will be:

List1.extend(List2)

Code Example

list_1 = ['Microsoft', 'Apple']
list_2 = ['Tesla']

list_1.extend(list_2)

print(list_1)

Output

['Microsoft', 'Apple', 'Tesla']
list_1 = ['Microsoft', 'Apple']
list_2 = ['Tesla', 'Devsheet', 'SpaceX']

#Join List_2 in List_1
for item in list_2:
  list_1.append(item)

print(list_1)

# -> ['Microsoft', 'Apple', 'Tesla', 'Devsheet', 'SpaceX']
We can use Python For loop and List.append() function to Join two lists. We are merging list_2 in list_1 using the above code snippet.
Was this helpful?
0 Comments