Course Content
Python Data Structures
Python Data Structures
Append() Method
Let's create a list named states
:
states = ['Texas', 'Florida', 'California', 'New Mexico'] print(states)
The list has four items. Imagine we want to add a fifth state, say Ohio. Here's how we do it:
states = ['Texas', 'Florida', 'California', 'New Mexico'] states.append('Ohio') print(states) print("4:", states[4])
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.
Task
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'
.
Task
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'
.
Everything was clear?
Append() Method
Let's create a list named states
:
states = ['Texas', 'Florida', 'California', 'New Mexico'] print(states)
The list has four items. Imagine we want to add a fifth state, say Ohio. Here's how we do it:
states = ['Texas', 'Florida', 'California', 'New Mexico'] states.append('Ohio') print(states) print("4:", states[4])
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.
Task
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'
.
Task
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'
.
Everything was clear?
Append() Method
Let's create a list named states
:
states = ['Texas', 'Florida', 'California', 'New Mexico'] print(states)
The list has four items. Imagine we want to add a fifth state, say Ohio. Here's how we do it:
states = ['Texas', 'Florida', 'California', 'New Mexico'] states.append('Ohio') print(states) print("4:", states[4])
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.
Task
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'
.
Task
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'
.
Everything was clear?
Let's create a list named states
:
states = ['Texas', 'Florida', 'California', 'New Mexico'] print(states)
The list has four items. Imagine we want to add a fifth state, say Ohio. Here's how we do it:
states = ['Texas', 'Florida', 'California', 'New Mexico'] states.append('Ohio') print(states) print("4:", states[4])
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.
Task
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'
.