python join and merge list 4 examples
Two list can be merge in python in many ways:
- listOne + listTwo
- listExtend.extend(listOne)
- [*listOne, *listTwo]
- list(set(listOne + listTwo))
You can watch video tutorial here: python join/merge list two and more lists
These are only some of the ways in which you can merge two list:
- Simple join of list Python 2 and 3
- Merge two list by extent function
- Join two list by Unpacking Generalizations
- Join two list removing duplicates
- Merge more than two list
- Performance tests
Simple join of list Python 2 and 3
The simplest and easiest to be read:
listOne = [1, 2, 3, 7, 9]
listTwo = [4, 5, 6, 7, 8]
listSimple = listOne + listTwo
print (listSimple)
result:
[1, 2, 3, 7, 9, 4, 5, 6, 7, 8]
Join two list by extent function
Using the standard extend method of python Collections
listOne = [1, 2, 3, 7, 9]
listTwo = [4, 5, 6, 7, 8]
listExtend = []
listExtend.extend(listOne)
listExtend.extend(listTwo)
print(listExtend)
result:
[1, 2, 3, 7, 9, 4, 5, 6, 7, 8]
Join two list by Unpacking Generalizations
It's new feature for all iterables in python since python 3.5
listOne = [1, 2, 3, 7, 9]
listTwo = [4, 5, 6, 7, 8]
joinedList = [*listOne, *listTwo]
print(joinedList)
result:
[1, 2, 3, 7, 9, 4, 5, 6, 7, 8]
Join two list removing duplicates
In case that you don't care about the order and want to remove all duplicate values you can use:
listOne = [1, 2, 3, 7, 9]
listTwo = [4, 5, 6, 7, 8]
listNoDuplicates = list(set(listOne + listTwo))
print(listNoDuplicates)
result:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Merge more than two list
In case that you don't care about the order and want to remove all duplicate values you can use:
listOne = [1, 2, 3, 7, 9]
listTwo = [4, 5, 6, 7, 8]
joinedList = [*listOne, *listTwo, *listOne, *listTwo, ]
print(joinedList)
result:
[1, 2, 3, 7, 9, 4, 5, 6, 7, 8, 1, 2, 3, 7, 9, 4, 5, 6, 7, 8]
Whole code example
4 examples of joining lists in python and a bonus of merging more than two lists:
#
# simple list merge
#
listOne = [1, 2, 3, 7, 9]
listTwo = [4, 5, 6, 7, 8]
listSimple = listOne + listTwo
print (listSimple)
#
# join list by extent
#
listExtend = []
listExtend.extend(listOne)
listExtend.extend(listTwo)
print(listExtend)
#
# merge list python 3.5 and above
#
joinedList = [*listOne, *listTwo]
print(joinedList)
#
# remove duplicates on merge
#
listNoDuplicates = list(set(listOne + listTwo))
print(listNoDuplicates)
#
# merge list more than 2
#
joinedList = [*listOne, *listTwo, *listOne, *listTwo, ]
print(joinedList)
result:
[1, 2, 3, 7, 9, 4, 5, 6, 7, 8]
[1, 2, 3, 7, 9, 4, 5, 6, 7, 8]
[1, 2, 3, 7, 9, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 7, 9, 4, 5, 6, 7, 8, 1, 2, 3, 7, 9, 4, 5, 6, 7, 8]
Performance tests
The simplest way is also the fastest one. Below you can find the test code and the results:
import pickle
import cProfile
def before():
for i in range(1, 1000000):
listOne = [1, 2, 3, 7, 9]
listTwo = [4, 5, 6, 7, 8]
listSimple = listOne + listTwo
def after():
for i in range (1, 1000000):
listOne = [1, 2, 3, 7, 9]
listTwo = [4, 5, 6, 7, 8]
listExtend = []
listExtend.extend(listOne)
listExtend.extend(listTwo)
def test():
for i in range (1, 1000000):
listOne = [1, 2, 3, 7, 9]
listTwo = [4, 5, 6, 7, 8]
joinedList = [*listOne, *listTwo]
cProfile.run('before()')
cProfile.run('after()')
cProfile.run('test()')
results:
4 function calls in 0.215 seconds
2000002 function calls in 0.602 seconds
4 function calls in 0.254 seconds