Learn Python in 30 Days — Day 10: Lists

Learn Python in 30 Days — Day 10: Lists

Welcome to Day 10 of the Learn Python in 30 Days series!

All example files for this series are available on my GitHub: Learn-Python-in-30-Days

Last time we explored while loops and saw how to repeat tasks until a condition changes.
Today, you’ll learn about one of Python’s most useful tools, lists and use them to build your very first Shopping List Manager.

Step 1 – What Is a List?

A list is like a digital container that holds multiple pieces of data — numbers, words, or even other lists.

groceries = ["milk", "bread", "eggs"] print(groceries)

Output:

['milk', 'bread', 'eggs']

You can store anything inside a list it’s one of Python’s most flexible data structures.

Try it yourself, hopefully you'll see something like that shown below: -

You can also download this example from my GitHub here and run it yourself.

Step 2 – Indexing (Accessing Items)

Each item in a list has a position (index).
Python starts counting at 0.

groceries = ["milk", "bread", "eggs"] print(groceries[0]) # milk print(groceries[2]) # eggs

Try changing a value:

groceries[1] = "butter" print(groceries)

Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.

Step 3 – Slicing (Getting Parts of a List)

Slicing allows you to extract sections of a list — like cutting a loaf of bread into smaller pieces.

The basic syntax is:

list_name[start:end]
  • start → the index to begin at (included)

  • end → the index to stop before (excluded)

Python doesn’t count the final position in the slice — this is called end-exclusive indexing.

Let’s see it in action:

numbers = [10, 20, 30, 40, 50] print(numbers[1:4]) # [20, 30, 40] print(numbers[:3]) # first three items print(numbers[-2:]) # last two items

Here’s what happens visually:

IndexValue
010
120
230
340
450
  • numbers[1:4] → starts at index 1 (20) and stops before 4[20, 30, 40]

  • numbers[:3] → starts from the beginning and goes up to (but not including) index 3[10, 20, 30]

  • numbers[-2:] → starts from the second last item and goes to the end → [40, 50]

You can even use a step value to skip through items:

numbers = [10, 20, 30, 40, 50, 60, 70] print(numbers[::2]) # every second item → [10, 30, 50, 70] print(numbers[1::2]) # every second item starting from index 1 → [20, 40, 60]

This is super useful when processing large data lists, generating sequences, or splitting text.

Tip: Negative slicing is powerful too.

print(numbers[::-1]) # reverses the list!

Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.

Step 4 – Adding & Removing Items

Lists are dynamic, meaning you can grow or shrink them freely — no need to declare sizes like in other programming languages.

Let’s begin:

groceries = ["milk", "bread"] groceries.append("eggs") # Add item to the end print(groceries)

Output:

['milk', 'bread', 'eggs']

Adding Items

There are several ways to add data:

# Add one item groceries.append("butter") # Add multiple items groceries.extend(["cheese", "apples"]) # Insert at a specific position groceries.insert(1, "coffee") # index 1, before 'bread'

Result:

['milk', 'coffee', 'bread', 'eggs', 'butter', 'cheese', 'apples']

Removing Items

You can remove items by name, index, or method:

groceries.remove("bread") # remove a specific item print(groceries) del groceries[0] # delete by index (0 = first) print(groceries) popped = groceries.pop() # removes and returns the last item print("Removed:", popped) print(groceries)

Clearing the List

If you ever want to start fresh:

groceries.clear() print(groceries) # []

Quick Recap

ActionMethodExample
Add one item.append(x)groceries.append("milk")
Add many.extend([...])groceries.extend(["eggs", "bread"])
Insert anywhere.insert(index, x)groceries.insert(0, "coffee")
Remove by value.remove(x)groceries.remove("bread")
Remove by indexdeldel groceries[2]
Remove last & return.pop()groceries.pop()
Clear all.clear()groceries.clear()

Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.

Step 5 – Looping Through a List

Combine what you learned from for loops:

groceries = ["milk", "bread", "eggs"] for item in groceries: print(f"- {item}")

Output:

- milk - bread - eggs

Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.

Mini-Project – Shopping List Manager

Now let’s use lists to build a real mini-program!

# Shopping List Manager shopping_list = [] while True: print("\nYour list:", shopping_list) print("Options: add, remove, quit") choice = input("What would you like to do? ").lower() if choice == "add": item = input("Enter item to add: ") shopping_list.append(item) print(f"{item} added!") elif choice == "remove": item = input("Enter item to remove: ") if item in shopping_list: shopping_list.remove(item) print(f"{item} removed.") else: print("Item not found.") elif choice == "quit": print("Final list:", shopping_list) break else: print("Invalid option.")

How it works

  • Starts with an empty list

  • Lets you add or remove items in a loop

  • Ends when the user types “quit”

You’ve just created an interactive, data-driven program! Hopefully you'll see something like that shown below: -

You can also download this example from my GitHub here and run it yourself.

Next up — Day 11 – Tuples & Sets

All example files for this series are available on my GitHub: Learn-Python-in-30-Days

You can see the full series here Learn Python in 30 Days series!

Hope you have enjoyed this post, thanks Matty

Comments

Popular posts from this blog

Math Behind Logic Gates

6502 - Part 2 Reset and Clock Circuit

Building a 6502 NOP Test