EN
Python - join two lists
0 points
In this article, we would like to show you how to join two lists in Python.
In this example, we use +
operator to join two lists.
Practical example:
xxxxxxxxxx
1
list1 = ['A', 'B']
2
list2 = ['C', 'D']
3
4
result = list1 + list2
5
6
print(result) # ['A', 'B', 'C', 'D']
Output:
xxxxxxxxxx
1
['A', 'B', 'C', 'D']
In this example, we use extend()
method to add list2
at the end of list1
.
Practical example:
xxxxxxxxxx
1
list1 = ['A', 'B']
2
list2 = ['C', 'D']
3
4
list1.extend(list2)
5
6
print(list1) # ['A', 'B', 'C', 'D']
Output:
xxxxxxxxxx
1
['A', 'B', 'C', 'D']
In this example, we use append()
method to append items from list2
at the end of list1
one by one.
Practical example:
xxxxxxxxxx
1
list1 = ['A', 'B']
2
list2 = ['C', 'D']
3
4
for x in list2:
5
list1.append(x)
6
7
print(list1) # ['A', 'B', 'C', 'D']
Output:
xxxxxxxxxx
1
['A', 'B', 'C', 'D']