Dictionaries and Sets
What You Will Learn
- Use dictionaries for key–value pairs.
- Use sets for collections of unique values.
- Create, initialize and access dictionaries and sets.
- Iterate through dictionary keys, values and key–value pairs.
- Add, remove and update dictionary entries.
- Compare dictionaries and sets.
- Combine sets with mathematical set operations.
- Use
inandnot in. - Modify mutable sets with set operators and methods.
- Create dictionaries and sets with comprehensions.
- Understand the basic idea of dynamic visualizations.
Chapter Structure
6.1 Introduction
Think about a normal dictionary. You look up a word and get its definition. Python dictionaries use the same basic idea: a key is associated with a value.
A dictionary is an unordered collection of key–value pairs. Its keys must be immutable and unique. A set is an unordered collection of unique immutable elements.
Key → Value
Example:
"India" → "in"Unique values only
Example:
{10, 20, 30}6.2 Dictionaries
A dictionary stores relationships between keys and values. Keys must be immutable—such as strings, numbers or tuples—and no two entries can have the same key.
| Example keys | Possible values |
|---|---|
| Country names | Internet country codes |
| Decimal numbers | Roman numerals |
| States | Lists of agricultural products |
| Hospital patients | Tuples of vital signs |
| Baseball players | Batting averages |
| Inventory codes | Quantity in stock |
6.2.1 Creating a Dictionary
Use curly braces {}. Each entry has the form key: value.
country_codes = {
'Finland': 'fi',
'South Africa': 'za',
'Nepal': 'np'
}
print(country_codes){'Finland': 'fi', 'South Africa': 'za', 'Nepal': 'np'}An empty dictionary is simply:
data = {}Dictionary Length and Empty Dictionaries
country_codes = {'Finland': 'fi', 'South Africa': 'za', 'Nepal': 'np'}
print(len(country_codes))
if country_codes:
print('country_codes is not empty')
else:
print('country_codes is empty')3
country_codes is not emptyAn empty dictionary evaluates to False; a non-empty dictionary evaluates to True.
6.2.2 Iterating through a Dictionary
A dictionary can be processed with a for loop. The items() method gives key–value pairs, which can be unpacked into two variables.
days_per_month = {
'January': 31,
'February': 28,
'March': 31
}
for month, days in days_per_month.items():
print(f'{month} has {days} days')January has 31 days
February has 28 days
March has 31 daysitems() → both key and value.6.2.3 Basic Dictionary Operations
Accessing a Value
roman_numerals = {
'I': 1,
'II': 2,
'III': 3,
'V': 5,
'X': 100
}
print(roman_numerals['V'])5Updating an Existing Value
roman_numerals['X'] = 10
print(roman_numerals){'I': 1, 'II': 2, 'III': 3, 'V': 5, 'X': 10}Adding a New Key–Value Pair
roman_numerals['L'] = 50
print(roman_numerals){'I': 1, 'II': 2, 'III': 3, 'V': 5, 'X': 10, 'L': 50}If the key does not already exist, assignment adds a new pair. String keys are case-sensitive, so accidentally using a different case can create a new key.
Removing a Key–Value Pair
del roman_numerals['III']You can also use pop(), which removes a key and returns its value:
value = roman_numerals.pop('V')
print(value)5Membership Testing
For dictionaries, in and not in test for keys.
codes = {'India': 'in', 'Japan': 'jp'}
print('India' in codes)
print('USA' in codes)True
False'in' on a dictionary checks keys, not values.6.2.4 Dictionary Methods keys and values
keys() provides the dictionary's keys, while values() provides its values.
grades = {'Ann': 90, 'Bob': 85, 'Cara': 90}
print(grades.keys())
print(grades.values())You can iterate through them:
for name in grades.keys():
print(name)
for grade in grades.values():
print(grade)keys() → names/labels; values() → stored data.6.2.5 Dictionary Comparisons
Dictionaries can be compared for equality and inequality. Two dictionaries are equal when they contain the same key–value pairs.
a = {'x': 1, 'y': 2}
b = {'y': 2, 'x': 1}
print(a == b)
print(a != b)True
False6.2.6 Example: Dictionary of Student Grades
A dictionary is a natural way to associate a student with a grade.
grades = {
'Susan': 92,
'Mike': 85,
'John': 78
}
print(grades['Susan'])
grades['John'] = 88
grades['Emma'] = 95
for name, grade in grades.items():
print(f'{name}: {grade}')The important idea is the mapping:
6.2.7 Example: Word Counts
Dictionaries are especially useful when counting how often something occurs. The word can be the key and its count can be the value.
text = 'red blue red green blue red'
counts = {}
for word in text.split():
if word in counts:
counts[word] += 1
else:
counts[word] = 1
print(counts){'red': 3, 'blue': 2, 'green': 1}If the key exists → increase its value.
If the key does not exist → create it with the starting value.
6.2.8 Dictionary Method update
update() adds key–value pairs from another mapping/iterable of pairs and can replace values for keys that already exist.
student = {'name': 'Aman', 'grade': 80}
student.update({'grade': 90, 'city': 'Delhi'})
print(student){'name': 'Aman', 'grade': 90, 'city': 'Delhi'}6.2.9 Dictionary Comprehensions
A dictionary comprehension is a compact way to build a dictionary from an iterable.
squares = {number: number ** 2 for number in range(1, 6)}
print(squares){1: 1, 2: 4, 3: 9, 4: 16, 5: 25}Read it as:
for loop, then learn the shorter comprehension form.6.3 Sets
A set is an unordered collection of unique elements. Duplicate values are automatically removed.
numbers = [0, 1, 2, 3, 4, 5, 0, 1, 2]
unique_numbers = set(numbers)
print(unique_numbers){0, 1, 2, 3, 4, 5}To create an empty set, use:
empty_set = set(){} creates an empty dictionary, not an empty set.Sets Are Iterable
colors = {'red', 'green', 'blue'}
for color in colors:
print(color)Because a set is unordered, do not depend on a particular iteration order.
Membership in a Set
colors = {'red', 'green', 'blue'}
print('red' in colors)
print('purple' not in colors)True
TrueFrozenset: An Immutable Set Type
Normal sets are mutable, so their contents can be changed. A frozenset is an immutable set. This makes it possible for a set to contain frozensets as elements.
fixed = frozenset([1, 2, 3])
print(fixed)6.3.1 Comparing Sets
Set comparison is based on membership, not on order.
{1, 3, 5} == {3, 5, 1}TrueSubset
< checks for a proper subset. <= checks for a subset that may also be equal.
{1, 3} < {1, 3, 5}
{1, 3} <= {1, 3, 5}
{1, 3, 5}.issubset({1, 3, 5})Superset
> checks for a proper superset. >= checks for a superset that may also be equal.
{1, 3, 5} > {1, 3}
{1, 3, 5}.issuperset({1, 3})| Operation | Meaning |
|---|---|
A == B | Same elements |
A != B | Different elements |
A < B | A is a proper subset of B |
A <= B | A is a subset of B |
A > B | A is a proper superset of B |
A >= B | A is a superset of B |
6.3.2 Mathematical Set Operations
Python provides the familiar mathematical operations: union, intersection, difference and symmetric difference.
Union — |
All unique elements from both sets.
{1, 3, 5} | {2, 3, 4}{1, 2, 3, 4, 5}Intersection — &
Only elements common to both sets.
{1, 3, 5} & {2, 3, 4}{3}Difference — -
Elements in the left set that are not in the right set.
{1, 3, 5} - {2, 3, 4}{1, 5}Symmetric Difference — ^
Elements that are in either set, but not in both.
{1, 3, 5} ^ {2, 3, 4}{1, 2, 4, 5}Disjoint Sets
Two sets are disjoint when they have no common elements.
{1, 2}.isdisjoint({3, 4})True| Symbol | Name | Easy meaning |
|---|---|---|
| | Union | Everything from both |
& | Intersection | Common elements |
- | Difference | Left only |
^ | Symmetric difference | In one, but not both |
6.3.3 Mutable Set Operators and Methods
The mathematical operators above create a new set. Mutable operations modify an existing set.
colors = {'red', 'green'}
colors |= {'blue', 'yellow'}
print(colors)The update() method also performs a union while modifying the set:
colors = {'red', 'green'}
colors.update(['blue', 'yellow'])
print(colors)Other mutable set operations include:
&=— update with intersection-=— update with difference^=— update with symmetric difference
Useful set methods also include add(), remove(), discard(), clear() and pop().
colors = {'red', 'green'}
colors.add('blue')
colors.remove('green')
print(colors)remove(): it expects the element to exist. When you specifically want a non-erroring removal for a missing element, discard() is useful.6.3.4 Set Comprehensions
Set comprehensions provide a concise way to create sets.
squares = {number ** 2 for number in range(1, 6)}
print(squares){1, 4, 9, 16, 25}Because the result is a set, duplicate results are kept only once.
6.4 Intro to Data Science: Dynamic Visualizations
The previous chapter introduced static visualization of die-roll results. This chapter takes the same general idea and makes the visualization dynamic.
Dynamic visualization: the graph keeps updating so you can watch the data change.
The chapter uses Seaborn and Matplotlib, together with Matplotlib's animation capabilities, to make the die-roll bar chart "come alive."
6.4.1 How Dynamic Visualization Works
A dynamic visualization is built from a sequence of animation frames. Each frame specifies what should change during one update.
Matplotlib's FuncAnimation drives these frame-by-frame updates. You define an update function and pass it to the animation system.
In the example, every frame:
- rolls the die a specified number of times,
- updates the die frequencies,
- clears the current plot,
- creates updated bars,
- creates updated frequency and percentage text.
The example uses roughly 30 frames per second when the interval is 33 milliseconds, although actual performance depends on the computer and work done in each frame.
6.4.2 Implementing a Dynamic Visualization
The dynamic die-roll script uses the same basic Seaborn and Matplotlib ideas from the previous chapter, but reorganizes them around animation.
Two Important Command-Line Arguments
| Argument | Meaning |
|---|---|
number_of_frames | How many animation frames will be displayed. |
rolls_per_frame | How many die rolls happen during each frame. |
For example:
ipython RollDieDynamic.py 6000 1This means 6000 frames and one roll per frame, for 6000 total rolls.
For a much faster simulation:
ipython RollDieDynamic.py 10000 600This performs 600 rolls per frame. Across 10,000 frames, that is 6,000,000 total rolls.
The update Function
FuncAnimation calls the update function once for each frame. The function receives the frame information and the values needed to update the visualization.
def update(frame_number, rolls, faces):
# roll dice
# update frequencies
# clear/update the plot
# draw bars
# display frequency and percentage text
passThe key idea is not memorizing every line of plotting code. Understand the loop:
Dynamic Visualization and the Law of Large Numbers
As more die rolls occur, the percentages tend to move toward the expected probability for each face. For a fair six-sided die, each face is expected to occur about 16.667% of the time.
With 6,000,000 rolls, the expected count for each face is about 1,000,000. Watching the bars and percentages approach these values makes the law of large numbers visually understandable.
Quick Concept Map
Dictionary → key → value
Dictionary operations → access → update → add → delete → search
Dictionary iteration → keys → values → items
Dictionary comprehension → compact dictionary creation
Set → unique values
Set comparison → equality → subset → superset
Set mathematics → union → intersection → difference → symmetric difference
Set mutation → add/remove/update
Set comprehension → compact set creation
Dynamic visualization → frames → update function → animated graph
Dictionary vs Set vs List
| Collection | Main idea | Typical use |
|---|---|---|
| List | Ordered sequence of elements | Store items where position matters |
| Dictionary | Key → value mapping | Look up data by a key |
| Set | Unique elements | Remove duplicates / membership / set mathematics |
Common Mistakes
- Using
{}when you want an empty set. Useset(). - Trying to use a mutable object as a dictionary key or set element.
- Forgetting that dictionary membership with
inchecks keys. - Depending on a dictionary/set display order.
- Expecting a set to keep duplicate values.
- Confusing union with intersection.
- Forgetting that
remove()expects the element to be present. - Trying to access a dictionary key that does not exist.
- Trying to memorize comprehension syntax before understanding the normal loop.
- Thinking a dynamic visualization is just a static plot—it repeatedly updates the display frame by frame.
Remember These Points
- Dictionary: key → value.
- Dictionary keys must be immutable and unique.
items()gives key–value pairs.keys()gives keys;values()gives values.inon a dictionary tests keys.- Set: unique elements.
set()creates an empty set.- Set order should not be relied upon.
|union,&intersection,-difference,^symmetric difference.FuncAnimationrepeatedly calls an update function to create animation.
Revision Questions
- What is a dictionary?
- What rules apply to dictionary keys?
- How do you create an empty dictionary?
- How do you access, update, add and delete a dictionary entry?
- What is the difference between
keys(),values()anditems()? - What does
intest when used with a dictionary? - Why are dictionaries useful for word counting?
- What is a dictionary comprehension?
- What is a set?
- How do you create an empty set?
- Why are duplicate values removed from a set?
- What is a subset? What is a superset?
- Explain union, intersection, difference and symmetric difference.
- What is the difference between a normal set and a frozenset?
- What is a dynamic visualization?
- What does
FuncAnimationdo? - What are
number_of_framesandrolls_per_frame?
Practice Programs
Create a dictionary containing five students and their grades. Print all students and grades, then update one grade.
Ask the user for a sentence and build a dictionary containing the count of every word.
Read a list containing duplicate numbers and use a set to produce only the unique values.
Create two sets of numbers and demonstrate union, intersection, difference and symmetric difference.
Create a dictionary mapping numbers 1–10 to their cubes.
Create a set containing the squares of numbers 1–10.
Chapter Wrap-Up
This chapter introduced two powerful built-in collections. Dictionaries organize data as key–value pairs, making them ideal for lookup and counting tasks. Sets store unique values and support useful mathematical operations such as union, intersection, difference and symmetric difference.
You also learned how comprehensions provide compact ways to create dictionaries and sets. Finally, the data-science section extended the earlier die-roll simulation into a dynamic visualization, where Matplotlib repeatedly updates the graph through animation frames. The visualization makes the convergence of die-roll frequencies toward their expected percentages easier to see.
Teaching note: This page is a simplified teaching edition based on the chapter organization and concepts in the supplied Python for Programmers source. Examples are rewritten for learning clarity rather than reproduced verbatim.