How to add two Lists in Python?

Hello Coders, In this blog, you will learn to add two lists in python. This is useful when we have to merge elements of two list into one list. We will merge this list in python in three different types lets discuss it in detail:- 

adding Lists in python

1. Merging two list using + Operator:-

In this method, We will simply make two lists list1 and list2. We will use + operator to list in python. We will create a list3 and will equal it to addition of list1 and list2. Now list3 is addition or merger of list1 and list2.

Plus.py

# Python code to concatenate two list using +
list1 = [1,‘two’,3,2,5]
list2 = [6,7,8,9,‘codeblockx’]
list3 = list1 + list2
print(” Addition of Two list is :”, list3)

OUTPUT

Addition of Two list is : [1, ‘two’, 3, 2, 5, 6, 7, 8, 9, ‘codeblockx’]

2. Merging two list using extend():-

In This Method, we will use extend() function to merge or add two lists in python. The syntax of this list_name.extend(list_name2). We will write the list_name of list which we want to extend and list_name2 which is to be attached to it. Then we can print the new merged list i.e list_name.  

extend.py

# python code to add two list using extend()
list1 = [5, 6, 8, 9, 10]
list2 = [1, 2, 3, 4,]
list2.extend(list1)
print(“Extended list is : “,list2)

OUTPUT

Extended list is : [1, 2, 3, 4, 5, 6, 8, 9, 10]

3. Merging two list using * Operator:-

In this method, we will use * operator to merge or add two lists. Firstly we will name two list list1 and list2. then in list3 we will follow the syntax [*listname, *listname2]. the final list list3 will be the addition or merger of list1 and list2. 

plus.py

# Python Code to add two lists using *
list1 = [1, 4, 6, 8]
list2  = [ 2, 5, 3, 9]
list3 = [*list1 , *list2]
print(“Addition of list using * is : “,list3)

OUTPUT

Addition of list using * is : [1, 4, 6, 8, 2, 5, 3, 9]

Hope this blog post was helpful to you and you have got the answer to your question. Thank you for visiting our blog. If you have any doubts about any coding questions then let us know in the comments section we will answer them as soon as possible. Till then check out our more blog posts.

Leave a Comment

Your email address will not be published. Required fields are marked *