Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Append() Method | List
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

Append() Method

Let's create a list named states:

12
states = ['Texas', 'Florida', 'California', 'New Mexico'] print(states)
copy

The list has four items. Imagine we want to add a fifth state, say Ohio. Here's how we do it:

1234
states = ['Texas', 'Florida', 'California', 'New Mexico'] states.append('Ohio') print(states) print("4:", states[4])
copy

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'.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 1. Chapter 7
toggle bottom row

Append() Method

Let's create a list named states:

12
states = ['Texas', 'Florida', 'California', 'New Mexico'] print(states)
copy

The list has four items. Imagine we want to add a fifth state, say Ohio. Here's how we do it:

1234
states = ['Texas', 'Florida', 'California', 'New Mexico'] states.append('Ohio') print(states) print("4:", states[4])
copy

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'.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 1. Chapter 7
toggle bottom row

Append() Method

Let's create a list named states:

12
states = ['Texas', 'Florida', 'California', 'New Mexico'] print(states)
copy

The list has four items. Imagine we want to add a fifth state, say Ohio. Here's how we do it:

1234
states = ['Texas', 'Florida', 'California', 'New Mexico'] states.append('Ohio') print(states) print("4:", states[4])
copy

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'.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Let's create a list named states:

12
states = ['Texas', 'Florida', 'California', 'New Mexico'] print(states)
copy

The list has four items. Imagine we want to add a fifth state, say Ohio. Here's how we do it:

1234
states = ['Texas', 'Florida', 'California', 'New Mexico'] states.append('Ohio') print(states) print("4:", states[4])
copy

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'.

Switch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 7
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt