Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Using the append() Method: Adding Elements to Lists | Mastering Python Lists
Python Data Structures
course content

Contenuti del Corso

Python Data Structures

Python Data Structures

2. Mastering Python Dictionaries
3. Mastering Python Tuples
4. Mastering Python Sets

book
Using the append() Method: Adding Elements to Lists

The append() method in Python is used to add an element to the end of a list. It's a convenient way to expand a list dynamically without having to create a new one.

Imagine you have a list of cities you want to visit. You've already listed a few, but as your plans evolve, you want to add more cities without replacing the existing ones.

python

The list currently contains four cities. Now imagine you want to add another city, say "Sydney". Here's how you can do it:

123456
travel_wishlist = ['Paris', 'Tokyo', 'New York', 'Rome'] # Adding a new city to the travel wishlist travel_wishlist.append("Sydney") print(travel_wishlist) # Output: ['Paris', 'Tokyo', 'New York', 'Rome', 'Sydney']
copy

As you can see, "Sydney" has been added to the end of the list without affecting the existing items.

Note

  • The list grows by one element every time you use append();
  • You can append not only strings but also variables, numbers, or even other lists;
  • The same logic applies to integers, floats, or any object.

The append() method allows you to efficiently update your list without recreating it. Additionally, this method is especially practical in for loops, where you can dynamically build or grow a list by adding elements iteratively based on conditions or computations.

Compito

Swipe to start coding

You are given a list of cities, travel_wishlist.

  • Add 2 cities to the list: "Berlin", "New York".
  • Use the append method instead of recreating the list.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 6
toggle bottom row

book
Using the append() Method: Adding Elements to Lists

The append() method in Python is used to add an element to the end of a list. It's a convenient way to expand a list dynamically without having to create a new one.

Imagine you have a list of cities you want to visit. You've already listed a few, but as your plans evolve, you want to add more cities without replacing the existing ones.

python

The list currently contains four cities. Now imagine you want to add another city, say "Sydney". Here's how you can do it:

123456
travel_wishlist = ['Paris', 'Tokyo', 'New York', 'Rome'] # Adding a new city to the travel wishlist travel_wishlist.append("Sydney") print(travel_wishlist) # Output: ['Paris', 'Tokyo', 'New York', 'Rome', 'Sydney']
copy

As you can see, "Sydney" has been added to the end of the list without affecting the existing items.

Note

  • The list grows by one element every time you use append();
  • You can append not only strings but also variables, numbers, or even other lists;
  • The same logic applies to integers, floats, or any object.

The append() method allows you to efficiently update your list without recreating it. Additionally, this method is especially practical in for loops, where you can dynamically build or grow a list by adding elements iteratively based on conditions or computations.

Compito

Swipe to start coding

You are given a list of cities, travel_wishlist.

  • Add 2 cities to the list: "Berlin", "New York".
  • Use the append method instead of recreating the list.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 6
Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?
some-alt