How to Join or append a List in python with example

Suppose you have two lists and output a single list by joining or appending multiple lists.

Each element is appended to the end of the new list.

Let’s create a list of sub-list or two-dimensional lists in python.

one = [1, 2,3,4]
two=[5,6,7,8,9]

After joining the list, the result is

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

Python joins two lists

There are multiple ways to join or append a list

  • use plus operator plus operator concatenates the list to another list.

Syntax:

one+two
or one+=two

Here is an example

one = [1, 2,3,4]
two=[5,6,7,8,9]

result=one+two

print(result)
  • use extend method extend method in a list appends one to another list

Here is an example

one = [1, 2,3,4]
two=[5,6,7,8,9]


result=[]
result.extend(one)
result.extend(two)
print(result)

one.extend(two)
print(one)
  • use operator Import operator into a code. operator add method appends a list and results in new list
import operator

one = [1, 2,3,4]
two=[5,6,7,8,9]

result = operator.add(one, two)
print(result)
  • extract and join using brackets syntax

unpack and extract individual elements and join using square bracket syntax

Here is an example

one = [1, 2,3,4]
two=[5,6,7,8,9]

result = [*one, *two]
print(result)