Lists are an important data structure in Python, used to store multiple elements in a single container. Python lists can store both similar types and heterogeneous types of elements. In Python, you can join or concatenate two or more lists. Joining a list merges numerous lists into a single list. This article explains the joining or concatenation of Python lists in several ways.
How to Join Python Lists
The following include methods for joining lists in Python:
- Using the append() function
- Using the extend() function
- Using the ‘+’ operator
- Using the ‘*’ operator
We will discuss these methods one by one.
Method 1: Using the append() Function
The append() function is a built-in function in Python that inserts elements to the end of a list. In the following example, we will use the append() function to join two lists.
myList1 = [1,2,3,4,5]
#creating list 2
myList2 = [6,7,8,9]
#joining lists 1 and 2 using append function
myList1.append(myList2)
#printing the new list
print(myList1)
Output
In the output, you will see that the first list has been added to another, to the end of the list as an item.
To join elements of lists, we need to iterate through list2 using the for loop and append each item separately to the end of the list1.
myList1 = [1,2,3,4,5]
#creating list 2
myList2 = [6,7,8,9]
#joining lists 1 and 2 using append function
for x in myList2:
myList1.append(x)
#printing the new list
print(myList1)
Output
Now, it can be seen that the lists are concatenated.
Similarly, we can join three lists using the append() function.
myList1 = [1,2,3,4,5]
#creating list 2
myList2 = [6,7,8,9]
#creating list 3
myList3 = ['kamran','Sattar','Awaisi']
#joining lists 1, 2, and 3 using the append function
for x in myList2:
myList1.append(x)
for x in myList3:
myList1.append(x)
#printing the list
print(myList1)
Output
Method 2: Using the extend() Function
The extend() function is the built-in function in Python that can be used to join lists. This function adds the elements of one list to the end of the other list. It is not necessary to iterate using loops with the
extend() function. In the following example, we will create two lists and join them using the extend() function.
#creating list 1
myList1 = [1,2,3,4,5]
#creating list 2
myList2 = [6,7,8,9]
#using the extend() function
myList1.extend(myList2)
#printing the list
print(myList1)
As you can see in the following output, the lists have been joined successfully.
Method 3: Using the ‘+’ Operator
Lists can also be joined using the ‘+’ operator. This is the easiest method for joining lists in Python. Lists that are joined using the ‘+’ operator are stored into a new list. In the following example, we will use the ‘+’ operator to join three lists.
myList1 = [1,2,3]
#creating list 2
myList2 = [4,5,6,7,8]
#creating list 3
myList3 = [1,3,4,6,7,8,4]
#joining the list using the '+' operator
myList1 = myList1+myList2+myList3
#printing the list
print("The joined list is: ",myList1)
Output
The following output shows the joined lists.
Method 4: Using the ‘*’ Operator
The ‘*’ operator can be also used to join Python lists. However, this feature is only supported by the Python 3.6+ version of Python. The joined lists are stored in the new list. Let’s use the ‘*’ operator to join the lists.
myList1 = [1,2,3]
#creating a list 2
myList2 = [4,5,6,7,8]
#creating a list 3
myList3 = [1,3,4,6,7,8,4]
#joining the list using the '*' operator
myList1 = [*myList1,*myList2,*myList3]
#printing the list
print("The joined list is: ",myList1)
Output
As you can see in the following output, the lists have been joined successfully using the ‘*’ operator.
Conclusion
The list in Python is a container that is used to store elements in a sequence. Python lists can be joined in several ways. This article explained how to join lists in Python with four basic methods through several simple examples.
from Linux Hint https://ift.tt/3qjRtcE
0 Comments