Last updated on February 2, 2023 by Ayaz Ali Shah
To add an item into a list you can use the append() method that pushes the item into the list. It has one required parameter which can be a string, a number or an object.
Let’s suppose we have a car list that contains three items that are stored into the
favoriteList variable and we need to add one more into it.
favoriteList = ["BMW", "Ferrari", "Rose Royce"]
To add the new item into the
favoriteList variable we need to attach the append() method with the variable and pass the string that needs to be added in the list.
To check if a string is added to the list we can use the print() function for printing the
favoriteList variables data.
print(favoriteList) # Output: ['BMW', 'Ferrari', 'Rose Royce', 'Tesla']
In the output we got all the items along with a new one.
In the second example let’s create a points list that contains integer values and store it points variable.
points = [10, 1, 100]
It has three items and we need to add one more to it. So attach the append() method to the points variable and pass the new item into append method as an argument.
Next we need to check if the new item is added.
print(points) # Output: [10, 1, 100, 50]
In the output, we get the list along with the new item.