To append multiple elements to a list in Python, the easiest way is with the + operator.
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)
#Output:
[1, 2, 3, 4, 5, 6]
You can also use the Python extend() function. extend() appends another list in place and returns None.
a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
print(a)
#Output:
[1, 2, 3, 4, 5, 6]
When working with collections of data, the ability to easily add items, remove items or change the values in a collection is valuable.
One such case where you want to add items is if you want to add multiple items to a list.
In Python, the easiest way to append multiple elements to a list is with the + operator.
If you have a list and want to add another list to it, then you get a new list which has all of the items from both lists.
Below is an example of how you can append multiple items to a list using + in Python.
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)
#Output:
[1, 2, 3, 4, 5, 6]
Using extend() to Append Multiple Items to List in Python
You can also use the extend() function to append multiple items to a list in your Python programs.
extend() appends another list in place and returns None.
Below is a simple example of how to use extend() to append elements to a list in Python.
a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
print(a)
#Output:
[1, 2, 3, 4, 5, 6]
Hopefully this article has been useful for you to learn how to append multiple items to a list using Python.