Hello everyone, in Join Two Lists Python tutorial, we will learn how to join two lists in python. Already, we know list is an important data structure of python. Joining two lists in python is very easy. So let’s see how to do them. But before proceeding further check the previous tutorial that explain you how to create a list in python and everything related to lists.
Join Two Lists Python Tutorial – An Easy Way To Join Lists
Meaning of Joining Lists
- Joining lists means, we combine the items of two lists into a list.
- It is also called merging or concatenation of lists.
Ways Of Joining Lists
In python, there is two ways by using that you can join two lists.
- Using + operator
- Using extend() method
Using + Operator
Let’s take an example, where lists are as follows –
1 2 3 4 |
List_1 = [1,9,0,7] List_2 = [5,8,3,6] |
Now we will join them.
1 2 3 4 5 6 7 8 |
List_1 = [1,9,0,7] List_2 = [5,8,3,6] List_3 = List_1 + List_2 print("Concatenation Of List_1 and List_2 : ", List_3) |
- Here we have taken two lists.
- Then we have joined these two lists and stored the result into an another list.
Output of this code is –
Now, if you don’t want to store the joined lists into a separate list then what will have you to do, let’s see.
1 2 3 4 5 6 7 8 |
List_1 = [1,9,0,7] List_2 = [5,8,3,6] List_1 = List_1 + List_2 print("Concatenation Of List_1 and List_2 : ", List_1) |
- Here we have stored the result of joined lists into List_1.
So the result is as follows.
Also Read : Linear Search Python – Learn Linear Search With Example
Using extend() method
Now we will see how to join two lists using extend() method. So understanding this we are taking an example. So let’s write the code for this.
- Using extend() method, we can’t store the concatenation of two lists into a third list.
- We can store the result only in an existing list.
1 2 3 4 5 6 7 8 9 10 11 |
List_1 = [1,9,0,7] List_2 = [5,8,3,6] List_3 = List_1.extend(List_2) print("Concatenation Of List_1 and List_2 : ", List_1) print("List_3 Items : ", List_3) print("List_2 Items : ", List_2) |
- Here the extend() function concatenate List_2 into List_1.
Now see the output of this code.
Here you can see List_3 has no items because using extend() method, we can’t store the concatenation of two lists into a third list.
Also Read : Python Queue Example : Implementation, Examples and Types
Taking Inputs From Users
Now we will see an example where we will take inputs from users.
So, for this we have to write the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
List1 = [] num1 = int(input("Enter Your 1st List's Size : ")) for n in range(num1): List1_items = int(input("Enter List's Items")) List1.append(List1_items) List2 =[] num2 = int(input("Enter Your 2nd List's Size : ")) for n in range(num2): List2_items = int(input("Enter List's Items")) List1.append(List2_items) Concatenated_List = List1 + List2 print("Concatenated List is: ", Concatenated_List) |
What We Did ?
- First of all we have initialized an empty list.
- Then we have asked the user to enter size of their list1.
- We have cast it to integer so user can only enter integers.
- Now we will need to ask the user to enter those numbers so for this we have started a for loop.
- Next, we have asked the user to enter items into list1.
- And then these numbers into list.
- We have repeated same procedure for second list.
- Then we have joined these two lists using + operator.
- And last simply printed the concatenated list.
And now, the output of this example is –
Also Read : Python Threading Example for Beginners
Dealing With Strings
Till now we have seen only examples of integers items of a list. And now we will see how to concatenate two lists if they contain string items. So for implementing this concept we have to write the following code.
1 2 3 4 5 6 7 8 |
List_1 = ['mango','banana'] List_2 = ['papaya'] List_3 = List_1+List_2 print("Concatenation Of List_1 and List_2 : ", List_3) |
- If we are joining two lists that contain string then we have to the follow same process as we do in the case of integers.
So that’s all for this Join Two Lists Python tutorial . You can leave your queries below in comment section to have a discussion about that. And please share this tutorial with your friends. Thank You.