Python Program to Concatenate Two Lists
Concatenation is the process of joining two or more strings or sequences of characters to create a single, longer string. In programming, concatenation is often used to combine text data that is stored in separate variables or arrays.
Python Code :
The below Python program concatenates two lists:
# Define two lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# Concatenate the lists using the + operator
concatenated_list = list1 + list2
# Print the concatenated list
print(concatenated_list)
Output:
[1, 2, 3, 4, 5, 6]
In this program, we define two lists called list1 and list2.
We then concatenate the two lists using the + operator, and assign the result to a new variable called concatenated_list. The + operator combines the elements of the two lists into a single list.
Finally, we print the concatenated list using the print() function.
Alternatively, you can use the extend() method to concatenate two lists:
# Define two lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# Concatenate the lists using the extend() method
list1.extend(list2)
# Print the concatenated list
print(list1)
Output:
[1, 2, 3, 4, 5, 6]
The extend() method adds the elements of list2 to list1, modifying list1 in place. This is an alternative to creating a new list, as we did with the + operator.