← Course Home
Module 2

List

Ordered collections — how you hold multiple values together and work with them one at a time or all at once.

Concept

Making a List

A list holds multiple values together, in order, inside square brackets, separated by commas.

groups = ["Engineering", "VPN-Users"]
print(groups)
Lab 1

Create a list fruits with "apple" and "banana", then print it.

fruits = ["apple", "banana"]
print(fruits)
Lab 2

Create a list counts with 1, 2, 3, then print it.

counts = [1, 2, 3]
print(counts)
Lab 3

Create an empty list called todo, then print it.

todo = []
print(todo)
[]
Lab 4

A list can mix types. Create mixed with "IT", 5, and True together, then print it.

mixed = ["IT", 5, True]
print(mixed)
Lab 5

Create groups with two department names of your choice, then print it.

e.g. groups = ["Sales", "IT"]
Now go practice

Open problems/01_create.py and do all 5 problems locally.

Concept

Printing a List

Printing a list shows its exact structure — brackets and quotes included. Printing a plain string doesn't show quotes; printing a list of strings does.

print("cat")      # cat
print(["cat"])    # ['cat']
Lab 1

Print ["a", "b", "c"] directly.

['a', 'b', 'c']
Lab 2

Print [1, 2, 3] — notice no quotes, since they're numbers.

[1, 2, 3]
Lab 3

fruits = ["apple", "banana"]. Print fruits[0] alone, then print fruits whole — compare.

apple then ['apple', 'banana']
Lab 4

Print an empty list.

[]
Lab 5

Print [True, 5] — a boolean and a number together.

[True, 5]
Now go practice

Open problems/02_print.py and do all 5 problems locally.

Concept

Getting an Item by Position

Each item in a list has a position, called an index, starting at 0 — not 1. list[0] gets the first item.

groups = ["Engineering", "VPN-Users"]
print(groups[0])   # Engineering
Lab 1

fruits = ["apple", "banana", "cherry"]. Print the first item.

apple
Lab 2

Print the second item (index 1).

banana
Lab 3

Print the third item (index 2).

cherry
Lab 4

Predict: what does fruits[0] print?

apple
Predict first

fruits only has 3 items (indexes 0, 1, 2). What happens with print(fruits[5])?

Check
CrashesIndexError: list index out of range

There's no item at position 5 — Python tells you instead of guessing.

Now go practice

Open problems/03_index_positive.py and do all 6 problems locally.

Concept

Getting the Last Item

Negative indexes count from the end. -1 is always the last item, -2 the second-to-last.

groups = ["Engineering", "VPN-Users"]
print(groups[-1])   # VPN-Users
Lab 1

fruits = ["apple", "banana", "cherry"]. Print the last item using a negative index.

cherry
Lab 2

Print the second-to-last item.

banana
Lab 3

Print fruits[-3] — what does it land on?

apple (the first item, counting back 3 from the end)
Lab 4

Compare: print fruits[0] then fruits[-3] — same list, same item.

→ both apple
Lab 5

Predict: fruits[-1] vs fruits[2] on a 3-item list — same or different?

Same — both land on cherry
Lab 6

Print the last item of a 1-item list ["only"] using [-1].

only
Now go practice

Open problems/04_index_negative.py and do all 6 problems locally.

Concept

Length

len() — the same function you used on strings — works on lists too. It counts the items.

groups = ["Engineering", "VPN-Users"]
print(len(groups))   # 2
Lab 1

Print len(["a", "b", "c"]).

3
Lab 2

Print len([]) — an empty list.

0
Lab 3

fruits = ["apple", "banana", "cherry"]. Print fruits[len(fruits) - 1] — the last item, the long way.

cherry
Lab 4

Compare len(fruits) to len("fruits") (the word) — different things being counted.

3 then 6
Lab 5

Print len([1, 2, 3, 4, 5]).

5
Lab 6 · Checkpoint

One script: create a list, print it, print its first item, its last item (negative index), and its length. Four print statements.

Combines sub-skills 1–5.
Now go practice

Open problems/05_length.py — 6 problems, #6 is a checkpoint combining sub-skills 1–5.

Concept

Checking If It's In There

in asks a yes/no question: is this value anywhere in the list? It answers with True or False — same idea as == and is from Module 1, just a different question.

Example
groups = ["Engineering", "VPN-Users"]
print("Engineering" in groups)   # True
print("Sales" in groups)         # False
Lab 1

fruits = ["apple", "banana"]. Print "apple" in fruits.

True
Lab 2

Print "cherry" in fruits.

False
Lab 3

Print "apple" not in fruits (the opposite check).

False
Lab 4

Print 5 in [1, 2, 3, 4, 5].

True
Lab 5

Print "x" in [] — checking an empty list.

False
Predict first

groups = ["Engineering", "VPN-Users"]. What does print(bool("Engineering" in groups)) print — is it different from just print("Engineering" in groups)?

Check
Same either way — True

in already gives back True/False directly, so wrapping it in bool() changes nothing.

Now go practice

Open problems/06_membership.py and do all 7 problems locally.

Concept

Adding to the End

.append() adds one item to the end of a list, without erasing anything already there.

groups = ["Engineering"]
groups.append("VPN-Users")
print(groups)   # ['Engineering', 'VPN-Users']
Lab 1

fruits = ["apple"]. Append "banana", print the result.

['apple', 'banana']
Lab 2

Append "cherry" too (two appends in a row), print.

['apple', 'banana', 'cherry']
Lab 3

Start with todo = []. Append "buy milk", print.

['buy milk']
Lab 4

After appending, print the list's len() to confirm it grew.

e.g. started at 1, now 2
Lab 5

Append a number to a list of numbers: [1, 2], append 3, print.

[1, 2, 3]
Lab 6

Append, then check membership: append "Sales" to groups, then print "Sales" in groups.

True
Lab 7

Append, then print the last item using [-1] to confirm it landed at the end.

matches what you appended
Lab 8

Append three different values in a row to an empty list, print the final list and its length.

length 3
Now go practice

Open problems/07_append.py and do all 8 problems locally.

Concept

Inserting at a Position

.insert(position, value) puts a new item at a specific spot — everything after it shifts over by one.

groups = ["Engineering", "VPN-Users"]
groups.insert(0, "Admins")
print(groups)   # ['Admins', 'Engineering', 'VPN-Users']
Lab 1

fruits = ["apple", "banana"]. Insert "cherry" at position 0, print.

['cherry', 'apple', 'banana']
Lab 2

Insert "kiwi" at position 1, print.

→ inserted between the first and second items
Lab 3

Insert at position 0 into an empty list, print.

→ single-item list
Lab 4

Compare: append "x" to one copy, insert "x" at position 0 into another copy — print both, notice the difference.

append → end, insert(0, ...) → start
Lab 5

Insert, then check the list's len() grew by 1.

Lab 6

Insert, then confirm with in that the new value is now present.

True
Lab 7

Insert at a position past the end, e.g. position 99 on a 2-item list — predict, then run.

No crash — Python just puts it at the end.
Now go practice

Open problems/08_insert.py and do all 7 problems locally.

Concept

Removing by Value

.remove(value) deletes the first item that matches the value you give it.

groups = ["Engineering", "VPN-Users"]
groups.remove("VPN-Users")
print(groups)   # ['Engineering']
Lab 1

fruits = ["apple", "banana", "cherry"]. Remove "banana", print.

['apple', 'cherry']
Lab 2

Remove the first item by value, print.

Lab 3

Remove, then check len() shrank by 1.

Lab 4

Remove, then confirm with in that the value is gone.

False
Predict first

fruits = ["apple", "banana"]. What happens with fruits.remove("cherry") — a value that isn't there?

Check
CrashesValueError: list.remove(x): x not in list

Same pattern as int("N/A") from Module 1 — an operation that only works if the thing you're asking for actually exists.

Lab 5

Before removing, check "cherry" in fruits first — a safe way to avoid the crash above (don't fix the crash, just observe the check).

False, so you'd know not to remove it
Now go practice

Open problems/09_remove.py and do all 7 problems locally.

Concept

Removing by Position

.pop() removes by position instead of value — and hands back what it removed, so you can use it. With no position given, it removes the last item.

groups = ["Engineering", "VPN-Users"]
removed = groups.pop()
print(removed)   # VPN-Users
print(groups)    # ['Engineering']
Lab 1

fruits = ["apple", "banana", "cherry"]. Pop the last item into a variable, print the variable.

cherry
Lab 2

After popping, print fruits — confirm it shrank.

['apple', 'banana']
Lab 3

Pop by position: fruits.pop(0) — removes and returns the first item instead.

apple
Lab 4

Pop, then check len() shrank by 1.

Lab 5

Compare: .remove("apple") (by value) vs .pop(0) (by position) — same result on ["apple", "banana"], different reasoning.

Both leave ['banana']
Lab 6

Pop from a 1-item list, print what was popped and the now-empty list.

→ the item, then []
Lab 7

Predict: what happens if you .pop() an empty list?

CrashesIndexError: pop from empty list
Lab 8 · Checkpoint

One script: append two items, remove one by value, pop the other, print what was popped, and print the final (now empty) list.

Combines sub-skills 6–10.
Now go practice

Open problems/10_pop.py — 8 problems, #8 is a checkpoint combining sub-skills 6–10.

Concept

Checking If It's Empty

An empty list is falsy — same truthiness rule from Module 1. bool(my_list) tells you whether it has anything in it.

groups = []
print(bool(groups))   # False — empty
Lab 1

Print bool([]).

False
Lab 2

Print bool(["apple"]) — one item.

True
Lab 3

Start with todo = ["task"], pop the only item, then print bool(todo).

False
Lab 4

Compare len(my_list) == 0 to bool(my_list) on the same empty list — same answer, different route.

True and False respectively, both correctly identify "empty"
Lab 5

Your fixture has 3 users with empty groups lists. Print bool([]) to confirm what that looks like.

False
Lab 6

Remove the only item from a 1-item list by value, print bool() of the result.

False
Now go practice

Open problems/11_empty_check.py and do all 6 problems locally.

Concept

Slicing a List

list[start:end] grabs a sub-range. The item at end is not included — it stops just before it.

fruits = ["apple", "banana", "cherry", "date"]
print(fruits[0:2])   # ['apple', 'banana'] — stops before index 2
Lab 1

fruits = ["apple", "banana", "cherry", "date"]. Print fruits[0:2].

['apple', 'banana']
Lab 2

Print fruits[1:3].

['banana', 'cherry']
Lab 3

Print fruits[:2] — leaving off the start means "from the beginning".

['apple', 'banana']
Lab 4

Print fruits[2:] — leaving off the end means "to the end".

['cherry', 'date']
Lab 5

Print fruits[:] — both left off, the whole list.

→ the full list
Predict first

fruits has 4 items. What does fruits[0:4] print — does it include the item at index 4?

Check
→ all 4 items — ['apple', 'banana', 'cherry', 'date']

There's no index 4 on a 4-item list (last is 3), so the "stop before end" rule just means it goes to the actual end. No crash, unlike indexing past the end with a single [].

Lab 6

Print fruits[1:1] — start and end the same.

[] — empty, nothing between position 1 and itself
Lab 7

Slice, then check the result's len().

Lab 8

Slice the first 2 items of a 5-item list of numbers, print the result.

Now go practice

Open problems/12_slicing.py and do all 8 problems locally.

Concept

Combining Two Lists

+ joins two lists into a new one — same symbol you used to combine strings in Module 1.

a = ["Engineering"]
b = ["VPN-Users"]
print(a + b)   # ['Engineering', 'VPN-Users']
Lab 1

a = ["apple"], b = ["banana"]. Print a + b.

['apple', 'banana']
Lab 2

Combine, then print the combined list's len().

2
Lab 3

Combine an empty list with a non-empty one, print the result.

just the non-empty one's items
Lab 4

Build one list via .append(), another as a literal, combine them with +, print.

Lab 5

Combine two lists, then check membership (in) of an item from the second list in the combined result.

True
Lab 6

Combine, then slice the first 2 items of the result.

Lab 7

Combine two lists, then pop the last item off the combined result.

Lab 8

Combine two 2-item lists, print the result and confirm its length is 4.

Now go practice

Open problems/13_combine.py and do all 8 problems locally.

Concept

Counting Repeats

.count(value) tells you how many times a value appears in a list.

groups = ["VPN-Users", "Engineering", "VPN-Users"]
print(groups.count("VPN-Users"))   # 2
Lab 1

letters = ["a", "b", "a", "c", "a"]. Print letters.count("a").

3
Lab 2

Print letters.count("z") — a value not present.

0 (no crash, unlike .remove() on a missing value)
Lab 3

Print letters.count("b").

1
Lab 4

Count on an empty list.

0
Lab 5

Append a duplicate value, then count it again — confirm it went up by 1.

Lab 6

Compare .count("a") > 0 to "a" in letters — same yes/no answer, different tool.

both True
Lab 7

Combine two lists that share a repeated value, then count it in the combined result.

Now go practice

Open problems/14_count.py and do all 7 problems locally.

Concept

Joining Into a String

.join() is backwards from the others — it's a string method, called on the separator, with the list passed in.

groups = ["Engineering", "VPN-Users"]
print(", ".join(groups))   # Engineering, VPN-Users
Lab 1

fruits = ["apple", "banana"]. Print ", ".join(fruits).

apple, banana
Lab 2

Join with a dash instead: "-".join(fruits).

apple-banana
Lab 3

Join with no separator: "".join(fruits).

applebanana
Lab 4

Join an empty list.

"" (empty string)
Lab 5

Join a 1-item list — no separator shows up with only one item.

→ just that one item
Predict first

nums = [1, 2, 3]. What happens with ", ".join(nums)?

Check
CrashesTypeError: sequence item 0: expected str instance, int found

.join() only works on a list of strings — same family of bug as gluing text onto None back in Module 1. Numbers need str() first (a Module 1 tool).

Lab 6 · Checkpoint

Build a list via .append(), combine it with another list using +, then .join() the result into one comma-separated line.

Combines sub-skills 7, 13, 15.
Lab 7

Join a list built from a slice: take fruits[0:2], join it with ", ".

Lab 8

Join a filtered-by-hand list: manually write a list containing only the fruits you'd keep, join it.

Lab 9

Print a full one-line report: create a list of 3 items, append a 4th, and .join() them into one printed sentence.

Lab 10 · Final checkpoint

One script using every sub-skill 1–15 at least once: create, print, index (both directions), length, membership, append, insert, remove, pop, empty-check, slice, combine, count, and join — ending in one printed summary line.

This is the big one — take your time.
Now go practice

Open problems/15_join.py — 10 problems, #10 is the final checkpoint for all of Module 2.

Complete

Module 2 done

create → print → index (+/-) → length → membership → append → insert → remove → pop → empty-check → slice → combine → count → join.

Run python check.py in problems/ to grade everything.