Looping Through Lists

STEP 3 — Looping Through Lists

(a.k.a. Checking Your Loot Like a Goblin Who Just Found Treasure)

You’ve got a list.
You’ve got items.
You’ve got power.

Now it’s time to loop through your list and look at every item like a dragon admiring its hoard.


1. The Classic For-Loop (a.k.a. “Show me EVERYTHING”)

If you want to see every item in your list, you don’t print them one by one like:

print(inventory[0])
print(inventory[1])
print(inventory[2])

Nope.
That’s how you summon boredom.

Instead, use a for loop:

inventory = ["Sword", "Potion", "Key", "Mega Banana"]

for item in inventory:
    print(item)

Boom.
You just built a scrolling inventory menu like a real game.

Click Run

2. Adding Some Drama (a.k.a. “Loot Reveal Animation”)

inventory = ["Sword", "Potion", "Key", "Mega Banana"]

for item in inventory:
    print("You found: " + item + "!")

Yes, even the Mega Banana gets hype.

Click Run

3. Looping With Indexes (a.k.a. “Numbering Your Loot Like a Pro”)

Sometimes you want numbers:

inventory = ["Sword", "Potion", "Key", "Mega Banana"]

for i in range(len(inventory)):
    print(str(i) + ": " + inventory[i])

Now your inventory looks like a real RPG menu.

Click Run

Practice (Your Turn!)

Mini-Challenge #1

Create a list called enemies with 4 enemy names.

Loop through the list and print:

A wild <enemy> appears!

Modify and Run

Mini-Challenge #2

Create a list called weapons.

Use a loop to print each weapon with a number in front, starting at 1.

Modify and Run

Have you ever think why did the Python list go to therapy?

Because it had too many issues to unpack.

(You’re welcome.)