Append() Method
Let's create a list named states
:
The list has four items. Imagine we want to add a fifth state, say Ohio. Here's how we do it:
The code above has added Ohio to the end of the list. The list now contains five items. states[4]
refers to Ohio.
If we want to add a number instead of a string, we don't use quotation marks. Here's how it's done:
The code above has added the number 10
to the end of the list. The list now includes six items. states[5]
refers to 10.
Note
The
append()
function allows you to add only one item at a time.
Tarea
You have a list named fruits
with these items:
'Raspberry', 'Pomegranate', 'Peach', 'Pear'
.
You need to add the following items to the existing list:
'Mango', 'Banana'
.
¿Todo estuvo claro?
Contenido del Curso
Python Data Structures
Python Data Structures
Append() Method
Let's create a list named states
:
The list has four items. Imagine we want to add a fifth state, say Ohio. Here's how we do it:
The code above has added Ohio to the end of the list. The list now contains five items. states[4]
refers to Ohio.
If we want to add a number instead of a string, we don't use quotation marks. Here's how it's done:
The code above has added the number 10
to the end of the list. The list now includes six items. states[5]
refers to 10.
Note
The
append()
function allows you to add only one item at a time.
Tarea
You have a list named fruits
with these items:
'Raspberry', 'Pomegranate', 'Peach', 'Pear'
.
You need to add the following items to the existing list:
'Mango', 'Banana'
.
¿Todo estuvo claro?