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 Ninja
course content

Course Content

Python Ninja

Python Ninja

1. Basic Controls
2. Advanced Controls
3. Functions
4. Loops
5. If-Else Statements
6. Challenges
7. Lists

book
Iterating 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 ninja.inventory.values(). Now, instead of relying on the position on the map, you can iterate through the ninja's inventory and perform actions on each element in it.

py

ninja.py

copy

Remember that ninja.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!

Section 7. Chapter 3
py

ninja.py

toggle bottom row

book
Iterating 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 ninja.inventory.values(). Now, instead of relying on the position on the map, you can iterate through the ninja's inventory and perform actions on each element in it.

py

ninja.py

copy

Remember that ninja.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!

Section 7. Chapter 3
Switch to desktopSwitch 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