list1 = [1, 2, 3] list2 = [4, 5, 6] concatenated_list = list1 + list2 # concatenated_list will be [1, 2, 3, 4, 5, 6]
In Python, you can concatenate two or more lists using the '+' operator.
Alternatively, you can use the extend() method to add the items of one list to another. For example:
list1 = [1, 2, 3] list2 = [4, 5, 6] list1.extend(list2) # list1 will be [1, 2, 3, 4, 5, 6]
*It's important to note that concatenation using the '+' operator creates a new list, while using extend() modifies the original list in place. This means that the '+' operator is more efficient when you want to create a new list from two existing lists, while extend() is more efficient when you want to modify an existing list.
Learn Flask development and learn to build cool apps with our premium Python course on Udemy.