Data Structures · Unit 3

Sets

Unordered collections without duplicates

Unit 3 · Data Structures

Choose the right container for your data

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.

Lists

Ordered and changeable

Tuples

Ordered and fixed

Sets

Unique values

Dictionaries

Key → value lookup

Unique values · changeable · no index

Sets: collections without duplicates

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.

Set operations lab

Edit two class subject lists, then compare their unique values.

Set A (3 unique)

PythonOSNetworks

Set B (3 unique)

OSData StructuresPython

Result · A | B (4 unique)

PythonOSNetworksData Structures

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.

Run the set comparison in Python
Starting up Python…
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))
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

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.

Avoid common set mistakes

  • An empty set is set(): {} creates an empty dictionary.
  • No positional access: subjects[0] is not valid for a set.
  • Set members must be hashable: strings and numbers work, but a mutable list cannot be a set member.
Sets check

1. What happens to repeated values when building a set?

2. Which operator finds values shared by A and B?

Compare the four structures

Ask two questions: does order matter, and should the collection change after creation?

StructureOrdered?Changeable?Access values by
listYesYesPosition (index)
tupleYesNoPosition (index)
setNo guaranteed orderYesMembership, not index
dictInsertion order is preservedYesKey

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.