Which method adds the specified list elements to the end of the current list in Python?

Python's .append[]: Add Items to Your Lists in Place

by Leodanis Pozo Ramos basics python
Mark as Completed
Tweet Share Email

Table of Contents

Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Building Lists With Python's .append[]

Adding items to a list is a fairly common task in Python, so the language provides a bunch of methods and operators that can help you out with this operation. One of those methods is .append[]. With .append[], you can add items to the end of an existing list object. You can also use .append[] in a for loop to populate lists programmatically.

In this tutorial, you’ll learn how to:

  • Work with .append[]
  • Populate lists using .append[] and a for loop
  • Replace .append[] with list comprehensions
  • Work with .append[] in array.array[] and collections.deque[]

You’ll also code some examples of how to use .append[] in practice. With this knowledge, you’ll be able to effectively use .append[] in your programs.

Free Download: Get a sample chapter from Python Basics: A Practical Introduction to Python 3 to see how you can go from beginner to intermediate in Python with a complete curriculum, up-to-date for Python 3.8.

Python - Add List Items

❮ Previous Next ❯

Append Items

To add an item to the end of the list, use the append[] method:

Example

Using the append[] method to append an item:

thislist = ["apple", "banana", "cherry"]
thislist.append["orange"]
print[thislist]
Try it Yourself »

Python List append[]

In this tutorial, we will learn about the Python list append[] method with the help of examples.

The append[] method adds an item to the end of the list.

Example

currencies = ['Dollar', 'Euro', 'Pound']
# append 'Yen' to the list currencies.append['Yen']
print[currencies]
# Output: ['Dollar', 'Euro', 'Pound', 'Yen']

Python List Refresher

Python lists can store zero or more items of a specific data type, such as strings, JSON objects, or numbers. For example, you can use a list to store a list of names or addresses. Lists are mutable. This means you can change their contents.

To create a new list in Python, you need to separate a list of values with commas and enclose it in square brackets []. Let’s create a list in Python, using this code:

toy_animals = ["Shark", "Dragon", "Bear", "Cat", "Giraffe"]

When we print out our list to the console, we can see the list that we’ve defined:

print[toy_animals]

Output:

["Shark", "Dragon", "Bear", "Cat", "Giraffe"]

Each item on our list has an index number, which can be used to reference an individual list item. These index numbers start with the value and increase for each item in a list. For our above toy_animals list, index numbers are assigned like this:

81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.

Find Your Bootcamp Match

The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.

Start your career switch today
» MORE: JavaScript Replace[]: A Step-By-Step Guide
“Shark”“Dragon”“Bear”“Cat”“Giraffe”
1234

The first item on our list Shark has the value 0. The last item Giraffe has the value 4. We can use these index numbers to access and manipulate our list. So, if we want to access the element with the index number 1, we can use the following code:

print[toy_animals[1]]

Output:

"Dragon"

Lists are mutable, which means that you can add and remove items to an existing list. Now that we’ve explored the basics of lists and list indexing in Python, we can start using the append[] and insert[] methods.

Python lists

last modified December 15, 2021

In this part of the Python programming tutorial, we cover Python lists in more detail.

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.

Methods to add elements to List in Python

There are four methods to add elements to a List in Python.

  1. append[]: append the object to the end of the list.
  2. insert[]: inserts the object before the given index.
  3. extend[]: extends the list by appending elements from the iterable.
  4. List Concatenation: We can use + operator to concatenate multiple lists and create a new list.

5. Data Structures¶

This chapter describes some things you’ve learned about already in more detail, and adds some new things as well.

Video liên quan

Bài mới nhất

Chủ Đề