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:-
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
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
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
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.