Accessing & Changing List Items

STEP 2 — Accessing & Changing List Items

(a.k.a. Digging Through Your Backpack Without Making a Mess)

You’ve got a list — your shiny new game backpack:

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

Now it’s time to reach inside, grab stuff, swap stuff, and maybe upgrade that banana into something less… questionable.


1. Accessing Items (a.k.a. “What’s in my bag?”)

Every item in a list has a position number called an index.

Python counts like this:

  • 0 → first item
  • 1 → second item
  • 2 → third item
  • 3 → fourth item

Yes, Python starts at zero.
No, we don’t know why.
Yes, it’s weird.
No, you can’t fight it.

Example

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

print(inventory[0])   # Sword
print(inventory[2])   # Key

Boom — you’re officially rummaging.

Click Run

2. Changing Items (a.k.a. Upgrading Your Gear)

Let’s say your banana has seen better days.

Time to replace it with something more heroic.

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

inventory[3] = "Mega Banana"

print(inventory)

Now your banana has evolved like a Pokémon with a gym membership.

Click Run

3. Why This Matters for Games

Accessing and modifying list items lets you:

  • upgrade weapons
  • change player gear
  • swap items in inventory
  • update enemy names
  • modify quest items
  • change dialogue lines

Basically, you’re now editing your game world like a developer, not a player.


Practice (Your Turn!)

Mini-Challenge

Create a list called loot with three items.

Then change the second item to “Golden Shield”.

Modify and Run
You just upgraded your loot like a pro.