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

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set
5. For deleting

bookInsert() Method

What if we want to add an item not at the end of the list, but at a specific position? For this purpose, we use the insert() method. Let's take a list called states as an example:

12
states = ['Washington', 'Florida', 'Georgia', 'California', 'New Mexico', 'Colorado'] print(states)
copy

This list contains six items:

StatementValue
states[0] 'Washington'
states[1]'Florida'
states[2]'Georgia'
states[3]'California'
states[4]'New Mexico'
states[5]'Colorado'

For instance, if we want to insert 'Hawaii' at the beginning of the list, we would:

123
states = ['Washington', 'Florida', 'Georgia', 'California', 'New Mexico', 'Colorado'] states.insert(0, 'Hawaii') print(states)
copy

After this, 'Hawaii' takes the 0 index. It's now at the top, and the rest of the items have shifted down. So, we now have 7 items:

StatementValue
states[0]'Hawaii'
states[1]'Washington'
states[2]'Florida'
states[3]'Georgia'
states[4]'California'
states[5]'New Mexico'
states[6]'Colorado'

Let's say we want 'New York' right before 'Georgia'. Since Georgia is at index 3, we'll give that index to New York.

Here's how:

123
states = ['Hawaii', 'Washington', 'Florida', 'Georgia', 'California', 'New Mexico', 'Colorado'] states.insert(3, 'New York') print(states)
copy

Previously, 'Georgia' was at index 3. Now 'New York' occupies that spot. Georgia and all items following it have moved down:

StatementValue
states[0]'Hawaii'
states[1]'Washington'
states[2]'Florida'
states[3]'New York'
states[4]'Georgia'
states[5]'California'
states[6]'New Mexico'
states[7]'Colorado'

Note

With the insert() function, you can add only one item at once.

Task

You have this list:

list_1 = ['ABBA', 'Aerosmith', 'The Animals', 'The Kinks']

You should modify it to:

list_1 = ['Kraftwerk', 'ABBA', 'Aerosmith', 'The Animals', 'New Order', 'The Kinks', 'The Orioles']

If you're unsure about the indices for inserting items, refer to the hints. Only use positive indexing. Insert 'Kraftwerk' first, 'New Order' second, and 'The Orioles' last.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 8
toggle bottom row

bookInsert() Method

What if we want to add an item not at the end of the list, but at a specific position? For this purpose, we use the insert() method. Let's take a list called states as an example:

12
states = ['Washington', 'Florida', 'Georgia', 'California', 'New Mexico', 'Colorado'] print(states)
copy

This list contains six items:

StatementValue
states[0] 'Washington'
states[1]'Florida'
states[2]'Georgia'
states[3]'California'
states[4]'New Mexico'
states[5]'Colorado'

For instance, if we want to insert 'Hawaii' at the beginning of the list, we would:

123
states = ['Washington', 'Florida', 'Georgia', 'California', 'New Mexico', 'Colorado'] states.insert(0, 'Hawaii') print(states)
copy

After this, 'Hawaii' takes the 0 index. It's now at the top, and the rest of the items have shifted down. So, we now have 7 items:

StatementValue
states[0]'Hawaii'
states[1]'Washington'
states[2]'Florida'
states[3]'Georgia'
states[4]'California'
states[5]'New Mexico'
states[6]'Colorado'

Let's say we want 'New York' right before 'Georgia'. Since Georgia is at index 3, we'll give that index to New York.

Here's how:

123
states = ['Hawaii', 'Washington', 'Florida', 'Georgia', 'California', 'New Mexico', 'Colorado'] states.insert(3, 'New York') print(states)
copy

Previously, 'Georgia' was at index 3. Now 'New York' occupies that spot. Georgia and all items following it have moved down:

StatementValue
states[0]'Hawaii'
states[1]'Washington'
states[2]'Florida'
states[3]'New York'
states[4]'Georgia'
states[5]'California'
states[6]'New Mexico'
states[7]'Colorado'

Note

With the insert() function, you can add only one item at once.

Task

You have this list:

list_1 = ['ABBA', 'Aerosmith', 'The Animals', 'The Kinks']

You should modify it to:

list_1 = ['Kraftwerk', 'ABBA', 'Aerosmith', 'The Animals', 'New Order', 'The Kinks', 'The Orioles']

If you're unsure about the indices for inserting items, refer to the hints. Only use positive indexing. Insert 'Kraftwerk' first, 'New Order' second, and 'The Orioles' last.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 8
toggle bottom row

bookInsert() Method

What if we want to add an item not at the end of the list, but at a specific position? For this purpose, we use the insert() method. Let's take a list called states as an example:

12
states = ['Washington', 'Florida', 'Georgia', 'California', 'New Mexico', 'Colorado'] print(states)
copy

This list contains six items:

StatementValue
states[0] 'Washington'
states[1]'Florida'
states[2]'Georgia'
states[3]'California'
states[4]'New Mexico'
states[5]'Colorado'

For instance, if we want to insert 'Hawaii' at the beginning of the list, we would:

123
states = ['Washington', 'Florida', 'Georgia', 'California', 'New Mexico', 'Colorado'] states.insert(0, 'Hawaii') print(states)
copy

After this, 'Hawaii' takes the 0 index. It's now at the top, and the rest of the items have shifted down. So, we now have 7 items:

StatementValue
states[0]'Hawaii'
states[1]'Washington'
states[2]'Florida'
states[3]'Georgia'
states[4]'California'
states[5]'New Mexico'
states[6]'Colorado'

Let's say we want 'New York' right before 'Georgia'. Since Georgia is at index 3, we'll give that index to New York.

Here's how:

123
states = ['Hawaii', 'Washington', 'Florida', 'Georgia', 'California', 'New Mexico', 'Colorado'] states.insert(3, 'New York') print(states)
copy

Previously, 'Georgia' was at index 3. Now 'New York' occupies that spot. Georgia and all items following it have moved down:

StatementValue
states[0]'Hawaii'
states[1]'Washington'
states[2]'Florida'
states[3]'New York'
states[4]'Georgia'
states[5]'California'
states[6]'New Mexico'
states[7]'Colorado'

Note

With the insert() function, you can add only one item at once.

Task

You have this list:

list_1 = ['ABBA', 'Aerosmith', 'The Animals', 'The Kinks']

You should modify it to:

list_1 = ['Kraftwerk', 'ABBA', 'Aerosmith', 'The Animals', 'New Order', 'The Kinks', 'The Orioles']

If you're unsure about the indices for inserting items, refer to the hints. Only use positive indexing. Insert 'Kraftwerk' first, 'New Order' second, and 'The Orioles' last.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

What if we want to add an item not at the end of the list, but at a specific position? For this purpose, we use the insert() method. Let's take a list called states as an example:

12
states = ['Washington', 'Florida', 'Georgia', 'California', 'New Mexico', 'Colorado'] print(states)
copy

This list contains six items:

StatementValue
states[0] 'Washington'
states[1]'Florida'
states[2]'Georgia'
states[3]'California'
states[4]'New Mexico'
states[5]'Colorado'

For instance, if we want to insert 'Hawaii' at the beginning of the list, we would:

123
states = ['Washington', 'Florida', 'Georgia', 'California', 'New Mexico', 'Colorado'] states.insert(0, 'Hawaii') print(states)
copy

After this, 'Hawaii' takes the 0 index. It's now at the top, and the rest of the items have shifted down. So, we now have 7 items:

StatementValue
states[0]'Hawaii'
states[1]'Washington'
states[2]'Florida'
states[3]'Georgia'
states[4]'California'
states[5]'New Mexico'
states[6]'Colorado'

Let's say we want 'New York' right before 'Georgia'. Since Georgia is at index 3, we'll give that index to New York.

Here's how:

123
states = ['Hawaii', 'Washington', 'Florida', 'Georgia', 'California', 'New Mexico', 'Colorado'] states.insert(3, 'New York') print(states)
copy

Previously, 'Georgia' was at index 3. Now 'New York' occupies that spot. Georgia and all items following it have moved down:

StatementValue
states[0]'Hawaii'
states[1]'Washington'
states[2]'Florida'
states[3]'New York'
states[4]'Georgia'
states[5]'California'
states[6]'New Mexico'
states[7]'Colorado'

Note

With the insert() function, you can add only one item at once.

Task

You have this list:

list_1 = ['ABBA', 'Aerosmith', 'The Animals', 'The Kinks']

You should modify it to:

list_1 = ['Kraftwerk', 'ABBA', 'Aerosmith', 'The Animals', 'New Order', 'The Kinks', 'The Orioles']

If you're unsure about the indices for inserting items, refer to the hints. Only use positive indexing. Insert 'Kraftwerk' first, 'New Order' second, and 'The Orioles' last.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 8
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt