Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Changing value by index | Lists
Python Ninja

bookChanging value by index

To modify a specific element, access the list at the desired index and assign it a new value. For example:

123456789
# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Change the second element inventory[1] = "parrot" # inventory is now ["cat", "parrot", "chicken", "monkey"] print(inventory)
copy

You can also change an item in the ninja's inventory by index. To do this, you can pass the index to the ninja.pick_to_inventory(index) method.

ninja.py

ninja.py

copy
Task

Swipe to start coding

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 7. ChapterΒ 2
ninja.py

ninja.py

index.html

index.html

preset.py

preset.py

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookChanging value by index

Swipe to show menu

To modify a specific element, access the list at the desired index and assign it a new value. For example:

123456789
# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Change the second element inventory[1] = "parrot" # inventory is now ["cat", "parrot", "chicken", "monkey"] print(inventory)
copy

You can also change an item in the ninja's inventory by index. To do this, you can pass the index to the ninja.pick_to_inventory(index) method.

ninja.py

ninja.py

copy
Task

Swipe to start coding

Solution

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Β 7. ChapterΒ 2
ninja.py

ninja.py

index.html

index.html

preset.py

preset.py

some-alt