Module 02 · Foundation

From one student to many

Represent several records with lists and dictionaries before building a table.

Before you start: Complete the dictionary examples in Module 1.

Python loads from this site on your first run.

Each run starts with fresh data. Download any exported files before another run. Your written answers and code drafts are saved on this device; export your work to keep a backup.

M02C01 · Explore · predict · practice

A list has positions

A list stores values in order. Positions start at 0, so names[1] is the second value. A slice such as [0:2] includes positions 0 and 1 but excludes position 2. A negative position counts back from the end.

Read the example

names = ["Amira", "Bilal", "Chloe", "Dana"]
print(names[0])
print(names[-1])
result = names[0:2]
print(result)
Verified example output · pandas 2.2.3
Amira
Dana
['Amira', 'Bilal']

Watch for this. A single position returns one item; a slice returns a list, even if it contains one item.

Your turn

Select only Bilal and Chloe into result using one slice. Then append Ethan and print the new list length.

For the code check, keep these result variable names: names, result. Different valid approaches are welcome.

Show a hint

Start the slice at position 1 and stop before position 3.

Show one worked solution
names = ["Amira", "Bilal", "Chloe", "Dana"]
result = names[1:3]
names.append("Ethan")
print(result)
print(len(names))

The slice returns Bilal and Chloe. append() adds a value at the end, so the full list now has five names.

Compare your explanations
  1. Read. They create a list containing four text values in order.
  2. Predict. Dana, then ["Amira", "Bilal"].
  3. Explain. The start position is included, but the stop position 2 is excluded.

One quick check

Which expression selects the second value?

What you should be able to explain. I can predict list positions and slices.

M02C02 · Explore · predict · practice

A dictionary of lists describes columns

Now each dictionary value is a list. The keys describe future column names, and values at the same position belong to the same student. data["Age"] chooses a list; the second pair of brackets chooses an item inside that list.

Read the example

data = {
    "Name": ["Amira", "Bilal", "Chloe"],
    "Age": [20, 21, 19],
    "Campus": ["RAK", "Dubai", "RAK"]
}
print(data["Name"])
print(data["Age"][1])
print(data["Name"][1], data["Campus"][1])
Verified example output · pandas 2.2.3
['Amira', 'Bilal', 'Chloe']
21
Bilal Dubai

Watch for this. Positions in lists are numbers; dictionary keys can be text. Do not swap their roles.

Your turn

Change Chloe's age from 19 to 20 without changing the other ages. Print Chloe's name and updated age.

For the code check, keep these result variable names: data. Different valid approaches are welcome.

Show a hint

Chloe is at position 2 inside each list.

Show one worked solution
data = {
    "Name": ["Amira", "Bilal", "Chloe"],
    "Age": [20, 21, 19],
    "Campus": ["RAK", "Dubai", "RAK"]
}
data["Age"][2] = 20
print(data["Name"][2], data["Age"][2])

The age list becomes [20, 21, 20]. Updating one nested list position does not change the other positions.

Compare your explanations
  1. Read. data is a dictionary. data["Age"] is a list.
  2. Predict. Bilal, age 21, at Dubai.
  3. Explain. Names could become paired with another student's age and campus. The lists must stay aligned by position.

One quick check

What does data["Age"][1] select?

What you should be able to explain. I can read a value from a dictionary containing lists.

M02C03 · Explore · predict · practice

A list of dictionaries describes rows

A second arrangement stores one dictionary per student inside a list. Each dictionary is a record, or row. rows[1] selects the second record; ["Name"] then selects a field within it. Both arrangements can later become a pandas DataFrame.

Read the example

rows = [
    {"Name": "Amira", "Age": 20, "Campus": "RAK"},
    {"Name": "Bilal", "Age": 21, "Campus": "Dubai"},
    {"Name": "Chloe", "Age": 19, "Campus": "RAK"}
]
print(rows[1])
print(rows[1]["Name"])
print(len(rows))
Verified example output · pandas 2.2.3
{'Name': 'Bilal', 'Age': 21, 'Campus': 'Dubai'}
Bilal
3

Watch for this. Count records with len(rows); len(rows[0]) counts fields in the first record.

Your turn

Add Dana, age 22, at Dubai as a fourth dictionary. Print the last student's name and the total record count.

For the code check, keep these result variable names: rows. Different valid approaches are welcome.

Show a hint

Use rows.append({...}) and then rows[-1]["Name"].

Show one worked solution
rows = [
    {"Name": "Amira", "Age": 20, "Campus": "RAK"},
    {"Name": "Bilal", "Age": 21, "Campus": "Dubai"},
    {"Name": "Chloe", "Age": 19, "Campus": "RAK"}
]
rows.append({"Name": "Dana", "Age": 22, "Campus": "Dubai"})
print(rows[-1]["Name"])
print(len(rows))

Dana is the last record and there are four records. This arrangement keeps each student's fields together.

Compare your explanations
  1. Read. One dictionary describing one student record.
  2. Predict. Bilal.
  3. Explain. rows is organized as a list of records, so select the record first. data was organized as named columns, so select the column first.

One quick check

Which arrangement stores one dictionary per row?

What you should be able to explain. I can tell column-oriented data from row-oriented records.

M02C04 · Explore · predict · practice

Check lengths before making a table

A rectangular table needs the same number of values in each plain list column. len() counts the values in a list. You can inspect all column lengths before constructing the DataFrame. A missing measurement still needs a position, often filled with None.

Read the example

data = {
    "Name": ["Amira", "Bilal", "Chloe"],
    "Age": [20, 21, 19]
}
lengths = {"Name": len(data["Name"]), "Age": len(data["Age"])}
same_length = lengths["Name"] == lengths["Age"]
print(lengths)
print("Ready for a rectangular table:", same_length)
Verified example output · pandas 2.2.3
{'Name': 3, 'Age': 3}
Ready for a rectangular table: True

Watch for this. None is a missing-value marker here; "None" in quotes is ordinary text.

Your turn

Add Dana to the Name list. Her age is unknown, so append None to Age. Confirm that the two lengths still match.

For the code check, keep these result variable names: data, same_length. Different valid approaches are welcome.

Show a hint

Append one value to each list; use None without quotes.

Show one worked solution
data = {
    "Name": ["Amira", "Bilal", "Chloe"],
    "Age": [20, 21, 19]
}
data["Name"].append("Dana")
data["Age"].append(None)
same_length = len(data["Name"]) == len(data["Age"])
print(data)
print("Matching lengths:", same_length)

Both lists now contain four positions. None marks the missing age while preserving the row alignment.

Compare your explanations
  1. Read. It compares two lengths and returns True or False. It does not assign a value.
  2. Predict. It becomes False: there would be three names but only two ages.
  3. Explain. Zero is a real numerical value. Using it could incorrectly suggest the student is zero years old and distort calculations.

One quick check

What is needed for a DataFrame built from plain list columns?

What you should be able to explain. I can check whether list-based columns form a consistent table.

Further detail: official documentation for this topic.