Skip to content

Instantly share code, notes, and snippets.

@prkhrv
Last active February 25, 2021 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prkhrv/1a9657dc21ae6e6ed2efcbf5658235c8 to your computer and use it in GitHub Desktop.
Save prkhrv/1a9657dc21ae6e6ed2efcbf5658235c8 to your computer and use it in GitHub Desktop.
Best ways to concatenate 2 or more lists in python
import timeit
mysetup = '''
import itertools
test_list1 = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_list2 = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_lista = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_list3 = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_list4 = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_listb = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_list5 = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_list6 = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_listc = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_list7 = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_list8 = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_listd = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_list9 = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_list10 = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_liste = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_list11 = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_list12 = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
test_listf = [1, 4, 5, 6, 5,5,56,56,5,6,3,45,4,10,23,45,56,67,78,89,12,122,22,22,22,22,22,33,44,44,2,1,3,3,3,3,3,3,3]
'''
test_case_1 = '''
for i in test_list2 :
test_list1.append(i)
'''
test_case_2 = '''
test_list3 + test_list4 + test_listb
'''
test_case_3 = '''
[y for x in [test_list5, test_list6] for y in x]
'''
test_case_4 = '''
test_list7.extend(test_list8)
test_list7.extend(test_listd)
'''
test_case_5 = '''
[*test_list9, *test_list10, *test_liste]
'''
test_case_6 = '''
list(itertools.chain(test_list11, test_list12, test_listf))
'''
print("Case 1")
print(timeit.timeit(setup = mysetup,stmt = test_case_1))
print("Case 2")
print(timeit.timeit(setup = mysetup,stmt = test_case_2))
print("Case 3")
print(timeit.timeit(setup = mysetup,stmt = test_case_3))
print("Case 4")
print(timeit.timeit(setup = mysetup,stmt = test_case_4))
print("Case 5")
print(timeit.timeit(setup = mysetup,stmt = test_case_5))
print("Case 6")
print(timeit.timeit(setup = mysetup,stmt = test_case_6))
++++++OUTPUT++++++
Case 1
1.3523006350005744
Case 2
0.30616794200250297
Case 3
1.4502678050012037
Case 4
0.28080460599812795
Case 5
0.25651553699935903
Case 6
1.2000570919990423
++++++++++++++++++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment