List append() Method in Python

Python List append() Method

In Python, the append() method is used to add an element to the end of a list. It is one of the built-in methods available for lists in Python and is quite straightforward to use.

Here's the basic syntax of the append() method:

list_name.append(element)
  • list_name: This is the name of the list to which you want to add the element.
  • element: This is the value you want to add to the list.

Let's see some examples of using the append() method:

Example 1:

# Create an empty list
my_list = []

# Append elements to the list
my_list.append(10)
my_list.append(20)
my_list.append(30)

print(my_list)  # Output: [10, 20, 30]

Example 2:

# Create a list with some initial elements
fruits = ['apple', 'banana', 'orange']

# Append a new fruit to the list
fruits.append('grapes')

print(fruits)  # Output: ['apple', 'banana', 'orange', 'grapes']

As you can see, the append() method is a convenient way to add elements to the end of a list, and it modifies the original list in place.