In Python, lists are a versatile and commonly used data structure that allow you to store and manipulate collections of items. Lists are also ordered, meaning the items in a list have a specific order. One common task when working with lists is adding multiple items to an existing list. In this tutorial, we will explore different ways to add multiple items to a list in Python.
Can you use the append function to add multiple items to a list?
Not directly. The append()
function is a built in list function in Python that lets you add a single item to the end of a list. You cannot pass multiple items or an iterable to the append()
function. Let’s look at an example.
# create a a list ls = [1, 2, 3] # add an item to the list ls.append(4) print(ls)
Output:
[1, 2, 3, 4]
You can see that we can use the append()
function to add a single element to a list. But what happens if you try to pass multiple elements or an iterable to the function.
# create a a list ls = [1, 2, 3] # add multiple items to the list ls.append(4, 5) print(ls)
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[2], line 4 2 ls = [1, 2, 3] 3 # add multiple items to the list ----> 4 ls.append(4, 5) 5 print(ls) TypeError: append() takes exactly one argument (2 given)
We get a TypeError
on passing multiple elements as the append()
function takes only a single argument.
# create a a list ls = [1, 2, 3] # add multiple items to the list ls.append([4, 5]) print(ls)
Output:
[1, 2, 3, [4, 5]]
Here, we passed the items to be added as a list, we didn’t get an error but the value appended to the original list is a list and not separate items.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
So we cannot directly use the append()
method to add multiple items but we can iterate over the items to be added one by one and then add them individually using the append()
method (see the example below).
Methods to add multiple items to a list
There are several methods that can be used to add multiple items to a list in Python. One of the most common methods is to use the extend()
method, which allows you to add multiple items to a list at once. Another method is to use the +=
operator, which concatenates two lists together. You can also use a for
loop to iterate over a sequence of items and add them to a list one by one using the append()
method.
Let’s look at each of these methods in detail with examples.
1) Using the extend()
method
The extend()
method in Python lists is used to add elements of one list to another list. It takes an iterable (e.g. list, tuple, string) as an argument and adds its elements to the end of the list. The original list is modified in place and the method returns None
.
Let’s look at an example.
# create a a list ls = [1, 2, 3] # add multiple items to the list ls.extend([4, 5]) print(ls)
Output:
[1, 2, 3, 4, 5]
You can see that here we used the extend()
method to add the elements 4 and 5 to the list ls
at once.
2) Using the +=
operator
The idea here is to concatenate a new list (with the items to be added) to the original list. The +
operator when applied to lists concatenates them. We use the compound operator +=
so that the resulting list from the concatenation is saved to the original list variable.
Let’s look at an example.
# create a a list ls = [1, 2, 3] # add multiple items to the list ls += [4, 5] print(ls)
Output:
[1, 2, 3, 4, 5]
The resulting list has the required elements added.
3) Using the append()
function iteratively
We saw earlier that the append()
function is used to add a single item to the end of a list. To add multiple items, iterate over the items to be added and then add them separately using the append()
function.
Let’s look at an example.
# create a a list ls = [1, 2, 3] # add multiple items to the list items = [4, 5] for item in items: ls.append(item) print(ls)
Output:
[1, 2, 3, 4, 5]
You can see that the list ls
now contains the required elements.
How to insert multiple elements at a specific index in a list?
We saw three methods to add multiple items to a list. Notice that there’s one thing common in all three methods. They all add the elements at the end of the list. But what if you want to insert multiple elements as a specific index in a list?
The insert()
list function is used to insert an element at a specific index in a list. It takes two arguments: the index at which the new element is to be inserted, and the element itself. All the elements after the specified index are shifted to the right.
Let’s see an example of using the insert()
function to add a single element at a specific index in a list.
# create a list ls = [1, 2, 3] # add 4 to the list at index 1 ls.insert(1, 4) print(ls)
Output:
[1, 4, 2, 3]
You can see that the element 4 was added at index 1 in the list.
Now, to add multiple elements in a list starting at a specific index, you can iterate over the elements to be added and insert them to the required index using the insert()
function.
Let’s see an example.
# create a list ls = [1, 2, 3] # add 4 and 5 to the list starting at index 1 items = [4, 5] # the index to start inserting the elements from index = 1 for item in items: ls.insert(index, item) # increment the index to maining the order index += 1 print(ls)
Output:
[1, 4, 5, 2, 3]
You can see that we added the elements 4 and 5 starting from index 1 in the list above. Note that after each insertion we are incrementing the index at which the insertion is to be performed by 1 so that the order of the elements to be added remains the same in the resulting list.
Conclusion
In this tutorial, we looked at how to add multiple elements to a list in Python. We explored three different methods to add multiple elements at the end of a list and a method to add elements at a specific index inside the list along with relevant examples.
You might also be interested in –