In Python, we can easily concatenate two lists using the ‘+’ operator.
Concatenation means joining two or more lists into a single list.
In this tutorial, we will see how we can concatenate two lists using Python.
Let’s take two lists as an example:
list1 = [1, 2, 3] list2 = [4, 5, 6]
Now, to concatenate these two lists, we simply use the ‘+’ operator:
concatenated_list = list1 + list2
The resulting concatenated_list will contain all the elements from list1 followed by all the elements from list2:
[1, 2, 3, 4, 5, 6]
Note that the original lists remain unchanged, and the concatenated list is a new list that contains all the elements of both lists.
Here’s the complete code for concatenating two lists:
# Define the two lists list1 = [1, 2, 3] list2 = [4, 5, 6] # Concatenate the two lists using the '+' operator concatenated_list = list1 + list2 # Print the concatenated list print(concatenated_list)
Output:
[1, 2, 3, 4, 5, 6]
That’s it!
Now you know how to concatenate two lists using Python.