Lists
Ordered and changeable
Unordered collections without duplicates
Unit 3 · Data Structures
A data structure organizes values so a program can find and work with them. Explore four built-in Python structures and use each lab to see how its rules affect real data.
Ordered and changeable
Ordered and fixed
Unique values
Key → value lookup
Unique values · changeable · no index
A set keeps one copy of each distinct, hashable value. It is useful for membership checks and mathematical comparisons. Sets do not provide a reliable positional order, so use a list when sequence matters.
Think of event check-in: a guest is either on the attendee list or not. Checking membership is easy, and scanning the same name twice does not create a second person.
Edit two class subject lists, then compare their unique values.
Set A (3 unique)
Set B (3 unique)
Result · A | B (4 unique)
Everything in either set. Duplicate entries in the text fields collapse into one set value.
The chips are arranged for readability only; Python sets do not guarantee this display order. The example sorts values when printing for stable output.
group_a = set(["Python","OS","Networks"])
group_b = set(["OS","Data Structures","Python"])
result = group_a | group_b
print("Unique in A:", sorted(group_a))
print("Unique in B:", sorted(group_b))
print("Result:", sorted(result))Output
Your program's output will appear here.
NameError: name 'mark' is not defined
NameError: A name was used before Python could find a variable or function with that name.
IndexError: list index out of range
IndexError: The requested position is outside the items in this list or sequence.
KeyError: 'email'
KeyError: That key does not exist in this dictionary.
TypeError: can only concatenate str (not "int") to str
TypeError: This operation does not work with the kind of value it received.
ZeroDivisionError: division by zero
ZeroDivisionError: A number cannot be divided by zero.
SyntaxError: invalid syntax
SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.
IndentationError: expected an indented block
IndentationError: The spaces at the start of a line do not match Python's block structure.
set(): {} creates an empty dictionary.subjects[0] is not valid for a set.1. What happens to repeated values when building a set?
2. Which operator finds values shared by A and B?
Ask two questions: does order matter, and should the collection change after creation?
| Structure | Ordered? | Changeable? | Access values by |
|---|---|---|---|
| list | Yes | Yes | Position (index) |
| tuple | Yes | No | Position (index) |
| set | No guaranteed order | Yes | Membership, not index |
| dict | Insertion order is preserved | Yes | Key |
Use a list for a sequence that changes, a tuple for a fixed record, a set for unique membership, and a dictionary for named lookups.