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

Cursusinhoud

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.

Taak

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 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.

Taak

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 6
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt