Dictionaries and sets
A dictionary quickly finds a value by its key, and a set stores unique elements. We study the purpose and operations of Python’s `dict` and `set`.
All topics (6 of 11)
Chapter 1. Algorithms
Chapter 2. Data structures
Chapter 3. Databases
Chapter 4. Modern technologies and projects
A dictionary (dict) is a structure of “key: value” pairs. In a list an element is found by its number (index), while in a dictionary it is found by its key: for example, a student’s grade by the student’s name. An empty dictionary is created with d = dict(), and a value is added and read with d["Ali"] = 5 and d["Ali"]. Keys are not repeated; if a new value is written under the same key, the old one is replaced.
Some operations come up often when working with a dictionary: "Ali" in d checks whether a key exists, d.get("Vali", 0) returns 0 if the key is missing, del d["Ali"] deletes, and d.items() loops over all the pairs. Searching by key in a dictionary is very fast because the place of the value is determined directly from the key. That is why dictionaries are used in counting tasks, for example to find how many times each word occurs in a text.
A set (set) is a collection of unique elements whose order does not matter. The expression set([1, 2, 2, 3, 3, 3]) holds only the elements 1, 2, 3, so a set is the shortest way to remove duplicates from a list. An element is added with s.add(4) and its presence is checked with 4 in s.
Sets support operations from mathematics: union a | b, intersection a & b, difference a - b. For example, if a = set([1, 2, 3]) and b = set([2, 3, 4]), then a & b is 2, 3 and a | b is 1, 2, 3, 4. The choice of structure depends on the task: an ordered sequence calls for a list, lookup by key for a dictionary, and removing duplicates with quick membership checks for a set.
Key takeaways
- A dictionary stores “key: value” pairs and finds a value quickly by its key.
- Dictionary keys are not repeated: a new value under the same key replaces the old one.
- A set stores unique elements and does not depend on order.
- Sets support union, intersection and difference.
- The structure is chosen to suit the task: a list, a dictionary or a set.
Practice tasks
- Create a dictionary whose keys are the names of three students and whose values are their informatics grades. Change one student’s grade and print all the pairs using
d.items(). - Write a program that uses a dictionary to count how many times each word occurs in a sentence entered by the user (hint: split the sentence into words with
split()and used.get(w, 0) + 1). - Two lists are given:
[1, 2, 3, 4, 4]and[3, 4, 5]. Use sets to find their common elements and their union, and remove duplicates from the first list.
Review questions
Test yourself: think first, then reveal the answer.
1How does a dictionary differ from a list?Show answerHide answer
In a list an element is accessed by its number (index), in a dictionary by its key.2How can duplicate elements be removed from a list?Show answerHide answer
Convert the list to a set: `set(a)` keeps only the unique elements.
0 of 11 topics completed
Sign in to mark