This article will show you how to add items to a list in Python using the append() method.
Lists are a type of Python variable that can hold any number of member elements. They’re analogous to arrays in other programming languages.
Lists can be defined and altered in several ways:
- Using the Python ‘filter()’ Function to Filter a List, with Examples
- Using the Python ‘map()’ Function to Process a List, With Examples
- Python List ‘sort()’ Method – Sorting Lists in Python Easily
- How to Reverse a List in Python
- Converting a List to a String (and Back!) in Python
But what if you want to add an item to the end of the list? That’s what the append() list method is for.
append() List Method Syntax
This is a simple method with a simple purpose – and it has simple syntax:
- list should be a list variable
- element should be the value or variable you wish to add to the end of the list
- The value will always be added to the end of the list, becoming the last value in the list but not replacing any existing values.
List append() method Example
Again, not a lot to be said for this one! Here’s the append() method in action:
cars = ['ford', 'oldsmobile', 'kia'] fruits.append('volvo')
Above, a cars variable containing a list of cars is defined. The append() method is then called from the list, passing a new value to be added to the end. The string value ‘volvo’ will be added to the end of the list, with the following resulting list:
['ford', 'oldsmobile', 'kia', 'volvo]