Join Two Lists Python – Learn Joining Two Lists With Examples

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.

Python List Tutorial

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 –

Now we will join them.

  • 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 –

Join Two Lists Python
Join Two Lists Python

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.

  • Here we have stored the result of joined lists into List_1.

So the result is as follows.

Join Two Lists Python
Join Two Lists Python

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.

  • Here the extend() function concatenate List_2 into List_1.

Now see the output of this code.

Join Two Lists Python
Join Two Lists Python

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.

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 –

Join Two Lists Python
Join Two Lists Python

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.

  • 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.
Join Two Lists Python
Join Two Lists Python

 

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.

Leave a Comment