Python Level 5: Lists & Dictionaries

Goal of this Level

Today you will learn to:

By the end, you will be able to write programs that store and manage multiple pieces of data at once.

Step 1: What is a List?

A list is a collection of multiple values.

Lists are used when you want to store many similar items.

Examples:


colors = ["red", "green", "blue"]

Explanation:

Step 2: Accessing Elements from a List

Each element in a list has a position (index).

Important: Python starts counting from 0.


colors = ["red", "green", "blue"]

print(colors[0])

Output:


red

Positions:


colors[0] → red
colors[1] → green
colors[2] → blue

Step 3: Adding Elements to a List

New elements can be added.

For that, use append().


colors = ["red", "green", "blue"]

colors.append("yellow")

Now the list contains:


["red", "green", "blue", "yellow"]

Step 4: Modifying Lists

You can also change elements.


colors = ["red", "green", "blue"]

colors[1] = "purple"

New list:


["red", "purple", "blue"]

Step 5: Looping Through a List

You can loop through a list using a loop.


colors = ["red", "green", "blue"]

for color in colors:
    print(color)

Output:


red
green
blue

Step 6: What is a Dictionary?

A dictionary stores data as key-value pairs.

That means:

key → value

Examples:


person = {"name":"Max", "age":20}

Explanation:

Step 7: Accessing Values in a Dictionary

To get a value, use the key.


person = {"name":"Max", "age":20}

print(person["name"])

Output:


Max

More examples:


person["age"] → 20

Step 8: Modifying Values in a Dictionary

You can also change values.


person = {"name":"Max", "age":20}

person["age"] = 21

Now the age is 21.

Practice Example

List example:


colors = ["red", "green", "blue"]
colors.append("yellow")
print(colors[0])

Dictionary example:


person = {"name":"Max", "age":20}
print(person["name"])

Task 1: Shopping List

Create a list for a shopping list.

Example structure:


shopping = ["Bread", "Milk", "Apples"]

Then print all the items.

Tip:

You can use a for loop.

Task 2: Pet Dictionary

Create a dictionary for a pet.

Information:

Example structure:


pet = {
"name":"Bello",
"species":"Dog",
"age":5
}

Then print the values:


print(pet["name"])
print(pet["species"])
print(pet["age"])