Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
7 views
in Python by (1.4k points)
edited by

E.x.

listA = [9,10,11]

listB = [12,13,14]

Expected outcome:

>>> concatenated list

[9,10,11,12,13,14]

3 Answers

+1 vote
by (46k points)
edited by

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.

+2 votes
by (10.9k points)
edited by

You can concatenate two lists using the ‘+’operator:

Ex-

listA = [9,10,11]

listB = [12,13,14]

concatenatedlist= listA+ listB

>>> concatenatedlist

[9,10,11,12,13,14]

0 votes
by (106k points)
edited by

You can directly do that using 

list1+list2

This gives a new list that is the concatenation of list1 and list2.

You can use the following video tutorials to clear all your doubts:-

Learn in detail about Python by enrolling in Intellipaat Python Course online and upskill.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...