DEV Community

Cover image for Python Day Three – Lists, Indices, and Packing Your Virtual Backpack πŸŽ’
Bonface Thuo
Bonface Thuo

Posted on

Python Day Three – Lists, Indices, and Packing Your Virtual Backpack πŸŽ’

Welcome back to Day 3, Python dynamic duo! πŸš€ If you survived Day 2, you now know how to create variables and throw strings, integers, floats, and booleans into their own little cardboard boxes. πŸ“¦
But what happens when you’re building a game and your character needs an inventory? Or you're making a shopping list app? Creating 50 different variables like item1, item2, item3 will make you want to throw your router out the window. πŸͺŸπŸ’»
Today, we are leveling up our storage game. We are moving out of single cardboard boxes and packing a Virtual Backpack: Enter Lists! πŸŽ’πŸŽ‰

πŸŽ’ What is a List?

In Python, a List is a data structure used to store a collection of items in one single variable. Think of it like a backpack where you can stuff multiple things inside, keep them in a specific order, and pull them out whenever you need them.
Creating a list is simple. You use square brackets [] and separate your items with commas:

# Packing our survival backpack πŸ—ΊοΈ
backpack = ["map", "flashlight", "water bottle", "protein bar"]

print(backpack) 
# Prints: ['map', 'flashlight', 'water bottle', 'protein bar']
Enter fullscreen mode Exit fullscreen mode

The coolest part? Python lists don’t care what you put inside. You can mix strings, integers, and booleans all in one single backpack (though usually, it makes the most sense to keep similar things together).

🀯 The First Rule of Coding Club: We Start Counting at Zero!
Here is where programming turns your brain upside down. πŸ§ πŸ™ƒ

If I asked you what the first item in our backpack list is, you’d logically say "map". And you'd be right in human language. But in Python-speak, computer memory starts counting at 0.

This is called Indexing.

To pull a specific item out of your backpack, you write the name of the list followed by the item's position (index) inside square brackets:

backpack = ["map", "flashlight", "water bottle", "protein bar"]

# Pulling out the items using their index πŸ”
print(backpack[0])  # Prints: map (The absolute first item!)
print(backpack[1])  # Prints: flashlight (The second item!)
print(backpack[3])  # Prints: protein bar
Enter fullscreen mode Exit fullscreen mode

⚠️ Tantrum Alert: If you try to print backpack[4], Python will immediately crash and scream:

IndexError: list index out of range.
Enter fullscreen mode Exit fullscreen mode


python
Why? Because there is no 5th item! Always remember: if your list has 4 items, the indices go from 0 to 3.

πŸ› οΈ Modifying the Backpack (List Methods)

The best thing about a backpack is that it isn’t glued shut. You can add things to it, change things inside it, or chuck things away when you don't need them anymore.

Here are the three most common magic spells you’ll use with lists:

1. Changing an item (Reassignment)
Did your flashlight break? Let's swap it out for a laser pointer:

backpack[1] = "laser pointer"
print(backpack)
# Prints: ['map', 'laser pointer', 'water bottle', 'protein bar']
Enter fullscreen mode Exit fullscreen mode

2. Adding an item (.append())
Found some gold coins on the floor? Let’s stuff them into the bottom of the backpack using .append():

backpack.append("gold coins")
print(backpack)
# Prints: ['map', 'laser pointer', 'water bottle', 'protein bar', 'gold coins']
Enter fullscreen mode Exit fullscreen mode

3. Removing an item (.remove())
Got hungry and ate the protein bar? We can remove it by name:

backpack.remove("protein bar")
print(backpack)
# Prints: ['map', 'laser pointer', 'water bottle', 'gold coins']
Enter fullscreen mode Exit fullscreen mode

Thats a wrap for the day ⏱️

πŸš€ Today's Challenge πŸ†

Time to put your virtual backpack to the test!

Create a list called gaming_squad containing the names of 3 of your friends (or fictional characters).

Print out the second person in that list (Remember the zero-counting rule! πŸ‘€).

Use .append() to add a 4th teammate to the squad.

Print the final list to the console to make sure they made the cut.

Drop your code or your squad lineup in the comments below! Are you starting to see how powerful these boxes can get? Tomorrow, we are looking at Tuplesβ€”which are basically lists that have been permanently superglued shut. πŸ”’

See you on Day 4! πŸπŸ’»πŸ‘‡

Top comments (0)