Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Iterating Through a List | Lists
Python Knight

bookIterating Through a List

Iterating through a list allows you to access and process each item individually. This is useful for performing actions on all items in your inventory.

A for loop can help you go through each item in the list.

123456
# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Iterate through the inventory for item in inventory: print("Item:", item)
copy

You can get a list of items in the inventory by writing knight.inventory.values(). Now, instead of relying on the position on the map, you can iterate through the knight's inventory and perform actions on each element in it.

knight.py

knight.py

copy

Remember that knight.put_from_inventory() places the last item from the inventory.

Task

Swipe to start coding

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 7. ChapterΒ 3
knight.py

knight.py

Ask AI

expand

Ask AI

ChatGPT

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

close

Awesome!

Completion rate improved to 2.94

bookIterating Through a List

Swipe to show menu

Iterating through a list allows you to access and process each item individually. This is useful for performing actions on all items in your inventory.

A for loop can help you go through each item in the list.

123456
# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Iterate through the inventory for item in inventory: print("Item:", item)
copy

You can get a list of items in the inventory by writing knight.inventory.values(). Now, instead of relying on the position on the map, you can iterate through the knight's inventory and perform actions on each element in it.

knight.py

knight.py

copy

Remember that knight.put_from_inventory() places the last item from the inventory.

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!

close

Awesome!

Completion rate improved to 2.94
SectionΒ 7. ChapterΒ 3
knight.py

knight.py

some-alt