Python .append() & .extend() Methods

We can add elements to a list using the append method. The append() method adds a single element towards the end of a list.

Example Of The .append() Method

For example, let’s extend the string by adding “April” to the list with the method append(). Using append() will increase the length of the list by 1.

list.append() adds a single element to a list

Using List Methods To Add Data: .append() Vs. .Extend()

The .append() method increases the length of the list by one, so if we want to add only one element to the list, we can use this method.

The .extend() method increases the length of the list by the number of elements that are provided to the method, so if we want to add multiple elements to the list, we can use this method.

Interactive Example Of The .append() & .extend() Methods

We start with the list names:

When we run the above code, it produces the following result:

We can then add two additional elements to the list names using extend() & the more_elementslist.

When we run the above code, it produces the following result:

RELATED LINKS

Data Scientist & Machine Learning Engineer