Lewati ke konten utama

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

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

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 months list by adding the element “April” 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)
print(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])
print(x)

# [1, 2, 3, 4, 5]

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

We start with appending a new element to the list names:

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

names.append('Amazon.com')
print(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.

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

names.append('Amazon.com')

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

# ['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.

Topik

Python courses

Kursus

Pengantar Python

4 Hr
6.8M
Kuasai dasar-dasar analisis data dengan Python dalam 4 jam. Kursus online ini memperkenalkan antarmuka Python dan pustaka populer.
Lihat DetailRight Arrow
Mulai Kursus
Lihat Lebih BanyakRight Arrow
Terkait

Tutorials

Python List Functions & Methods Tutorial and Examples

Learn about Python List functions and methods. Follow code examples for list() and other Python functions and methods now!
Abid Ali Awan's photo

Abid Ali Awan

Tutorials

18 Most Common Python List Questions

Discover how to create a list in Python, select list elements, the difference between append() and extend(), why to use NumPy and much more.
Karlijn Willems's photo

Karlijn Willems

Tutorials

Python Concatenate Strings Tutorial

Learn various methods to concatenate strings in Python, with examples to illustrate each technique.
DataCamp Team's photo

DataCamp Team

Tutorials

Python Append String: 6 Essential Concatenation Techniques

Explore six key methods for string concatenation in Python, using consistent examples to highlight the syntax and application of each technique.
Adel Nehme's photo

Adel Nehme

Tutorials

Python Dictionary Append: How to Add Key-Value Pairs

Learn how to append key-value pairs in a Python dictionary using methods such as square bracket notation, .update() for bulk additions, and .setdefault() for conditional inserts.
Samuel Shaibu's photo

Samuel Shaibu

Tutorials

Pandas Add Column Tutorial

You are never stuck with just the data you are given. Instead, you can add new columns to a DataFrame.
DataCamp Team's photo

DataCamp Team

Lihat Lebih BanyakLihat Lebih Banyak