Updated March 31, 2023
Introduction to Python Join List
In this article, we are discussing how to join lists in Python. In Python, the join list means joining or adding, or concatenating lists. To do this in Python join() method is introduced. This join() method is used to join each element of the given list by using delimiter or separator. Usually, join() method is a string method that uses a string separator or delimiter and this method returns the elements of the list in a sequence which is obtained by joining the elements by providing string separator or delimiter. In Python, join lists can be done in many ways using “+” operator or append() function or extend() function or join() function. These ways of joining lists can be seen in the below article.
Working of Python Join List with Examples
In this article, joining or concatenating lists in Python means joining or adding, or concatenating the given lists of any data types. To do this there are different ways. Let us see below with examples.
Now let us see how to join lists using the “+” operator which is the simple and easiest way of joining given lists. Let us consider the below example:
Example #1
Code:
print("The below program uses + operator for joining the given lists")
print("\n")
l1 = ["a", "b" , "c"]
print("The first list is given as follows:")
print(l1)
print("\n")
l2 = [1, 2, 3]
print("The second list is given as follows:")
print(l2)
print("\n")
res = l1 + l2
print("The concatenated or joined list is obtained as follows:")
print(res)
Output:
In the above program, we can see two different lists one with the character data type and the second list with the numerical data type. These two lists are joined using the “+” operator which can be seen in the statement “res = l1 + l2” where the concatenated or join the list is obtained and stored in the variable “res”. The output is as shown in the above screenshot.
Now let us see how to join the list using the join() method when the given separator is specified to form different lists from the original list. Let us consider an example below:
Example #2
Code:
print("The below program uses join() method for joining list with specified separator:")
print("\n")
l = ['1', '2', '3', '4']
print("The given list is as follows:")
print(l)
print("\n")
sptr = '_'
print("The obtained list after joining the given list with given separator")
print(sptr.join(l))
Output:
In the above program, we can see the given list is joined with a given separator “_” using join() method. The output is as shown in the above screenshot. Therefore join() method is a string method and is not usually used in lists. But this join() method is used in Python because it can be used with any iterable data structure like lists, tuples, etc, and at the end, this method returns string hence the join() method is known as the string method. It is better to avoid join() method as it can be used reverse for splitting which is confusing and we should also note that it is used only on string literals and only used when any special character is needed to add to the given list as specifying this special character as separator or delimiter to obtain the proper list.
Now let us see how to join lists using the append function in Python. In this, we use the append() function which is used for appending all the elements in the given two lists. Let us consider the below example.
Example #3
Code:
print("The below program uses append() function for joining lists")
print("\n")
l1 = ["a", "b" , "c"]
print("The first list is given as follows:")
print(l1)
print("\n")
l2 = [1, 2, 3]
print("The second list is given as follows:")
print(l2)
print("\n")
for i in l2:
l1.append(i)
print("The joined or appended list is obtained as follows:")
print(l1)
Output:
In the above program, we can see we have two different lists with two different data types. To join these lists we have used append() function. This function appends the second list to the first list that is done by joining the first element of the second list to the last element of the first list. The output is as shown in the above screenshot. Hence this function can also be used for joining the given lists.
Now let us how the extend() function is used for adding elements from one list to another list. This function is also similar to the append() function. Let us consider the below example.
Example #4
Code:
print("The below program uses extend() function for joining lists")
print("\n")
l1 = ["d", "e" , "f"]
print("The first list is given as follows:")
print(l1)
print("\n")
l2 = [5, 8, 9]
print("The second list is given as follows:")
print(l2)
print("\n")
l1.extend(l2)
print("The joined or concatenated list from the given lists is obtained as follows:")
print(l1)
Output:
In the above program, we can see we have again two different lists with two different data types. In this, we use extend() function to join the given lists. This function adds all the elements of the second list at the end of the first list.
Conclusion
In this article, we have seen what is joining the lists in Python means and in what ways it can be done. We also saw the “+” operator is used for joining the given lists. We also saw how to join()method is used and why it can be not used for joining two different lists but only can be used on string literals with separator or delimiter specified. We also saw how to join lists using the append() function and lastly, we saw the use of extend () function for joining lists.
Recommended Articles
This is a guide to Python Join List. Here we discuss the Working of Python Join List and Examples along with the codes and outputs. You may also have a look at the following articles to learn more-