There are several methods to perform this task:
Method 1:
Just use the * operator to concentrate two lists in Python (Only works in Python 3.6+)
Input:-
# Initializing lists
test_list1 = [9, 10, 11]
test_list2 = [12, 13, 14]
# using * operator to concat
res_list = [*test_list1, *test_list2]
# Printing concatenated list
print ("Concatenated list using * operator : "
+ str(res_list))
Output:-
[9,10,11,12,13,14]
Method 2:
You can use Native Method:
You just have to traverse the second list and keep appending elements in the first list, this will result the elements in both lists and perform the append.
Syntax:
# concatenation using naive method
# Initializing lists
test_list1 = [ 9, 10, 11]
test_list2 = [12, 13, 14]
# using naive method to concat
for i in test_list2 :
test_list1.append(i)
# Printing concatenated list
print ("Concatenated list using naive method : "
+ str(test_list1))
Output:
[9, 10, 11, 12, 13, 14]
Method 3:
Using extend()
You can also use extend() to concatenate two lists in Python:
Input:
# Initializing lists
test_list3 = [9, 10, 11]
test_list4 = [12, 13, 14]
# using list.extend() to concat
test_list3.extend(test_list4)
# Printing concatenated list
print ("Concatenated list using list.extend() : "
+ str(test_list3))
Output:
[9, 10, 11, 12, 13, 14]
You can find more methods to perform the same command but this is the fastest, easiest and shortest method to perform it.
Be a Python Expert. Enroll in Python Training by Intellipaat.