Skip to main content
HomeAbout PythonLearn Python

Python .append() and .extend() Methods Tutorial

Learn how to use the .append() and .extend() methods to add elements to a list.
Jun 2020  · 2 min read

You 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

months = ['January', 'February', 'March']
months.append('April')
print(months)
['January', 'February', 'March', 'April']

Using list methods to add data: append() vs. extend()

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

x = [1, 2, 3]
x.append(4)
x
[1, 2, 3, 4]

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

x = [1, 2, 3]
x.extend([4, 5])
x
[1, 2, 3, 4, 5]

Interactive example of the append() and extend() methods

We start with the list names:

names =  ['Apple Inc', 'Coca-Cola', 'Walmart']
# Append a name to the list names
names.append('Amazon.com')
print(names)

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

names =  ['Apple Inc', 'Coca-Cola', 'Walmart', 'Amazon.com']

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

# Extend list names
more_elements = ['DowDuPont', 'Alphabet Inc']
names.extend(more_elements)
print(names)

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

['Apple Inc', 'Coca-Cola', 'Walmart', 'Amazon.com', 'DowDuPont', 'Alphabet Inc']

Try it for yourself.

To learn more about list methods and functions, please see this video from our course Introduction to Python for Finance.

This content is taken from DataCamp’s Introduction to Python for Finance course by Adina Howe.

Check out our Python For Finance Tutorial.

Topics

Python courses

Certification available

Course

Introduction to Python

4 hr
5.4M
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

A Data Science Roadmap for 2024

Do you want to start or grow in the field of data science? This data science roadmap helps you understand and get started in the data science landscape.
Mark Graus's photo

Mark Graus

10 min

Python NaN: 4 Ways to Check for Missing Values in Python

Explore 4 ways to detect NaN values in Python, using NumPy and Pandas. Learn key differences between NaN and None to clean and analyze data efficiently.
Adel Nehme's photo

Adel Nehme

5 min

Seaborn Heatmaps: A Guide to Data Visualization

Learn how to create eye-catching Seaborn heatmaps
Joleen Bothma's photo

Joleen Bothma

9 min

Test-Driven Development in Python: A Beginner's Guide

Dive into test-driven development (TDD) with our comprehensive Python tutorial. Learn how to write robust tests before coding with practical examples.
Amina Edmunds's photo

Amina Edmunds

7 min

Exponents in Python: A Comprehensive Guide for Beginners

Master exponents in Python using various methods, from built-in functions to powerful libraries like NumPy, and leverage them in real-world scenarios to gain a deeper understanding.
Satyam Tripathi's photo

Satyam Tripathi

9 min

Python Linked Lists: Tutorial With Examples

Learn everything you need to know about linked lists: when to use them, their types, and implementation in Python.
Natassha Selvaraj's photo

Natassha Selvaraj

9 min

See MoreSee More