Module 4 of 6, Python Foundations
Dictionaries and Sets
Look values up by key, keep unique items, and nest collections to model real data.
module checksSign in to track
- Lessons complete-, not done
- Lesson quizzes taken-, not done
- Module Labs passed-, not done
- Module exam passed at 80%-, not done
- lessons
- 4
- quiz questions
- 48
- reading time
- 32 min
- exam length
- 15 Q
- coding exercises
- 10
Lessons
- Dictionaries & Hash MapsLook values up by name instead of position with dictionaries: add, change and remove entries, read safely with get(), loop over keys, values and items, and count things. 8 min 12 questions
- Set Creation & DeduplicationKeep only unique values with sets: remove duplicates, test membership quickly, combine groups with union, intersection and difference, and see how sets and dictionary keys relate. 8 min 12 questions
- Nested Data StructuresCombine collections to model real data: grids as lists of lists, tables as lists of dictionaries, and groups as dictionaries of lists, read with chained indexes and safe lookups. 8 min 12 questions
- Collections Module ToolsUse the collections module's ready-made tools: Counter for counting, defaultdict for grouping without KeyError, deque for fast queues, and namedtuple for readable records. 8 min 12 questions
Lab: write it yourself
Real Python, real checks: write your answer, press Run checks, and keep going until it passes. (6 exercises)
The Lab needs an account. Sign in or create a free ID to run these exercises.
Lab 1: Config lookup
Given config = {"host": "localhost", "port": 5432}, print Server running on localhost:5432 using values from the dictionary.
Sign in to practise
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Lab 2: Update fruit stock
Given stock = {"apple": 10, "orange": 5}, add 7 more apples and add "banana": 12. Print: {'apple': 17, 'orange': 5, 'banana': 12}.
Sign in to practise
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Lab 3: Shared interests
Find common items between a = {"python", "sql", "react"} and b = {"sql", "docker", "python"}. Print the sorted list: ['python', 'sql'].
Sign in to practise
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Lab 4: Letter frequency
Count occurrences of each character in "mississippi" using a dictionary. Print the count of 's': s: 4.
Sign in to practise
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Lab 5: Unique visitors
The log ["Mim", "Rafi", "Mim"] has repeats. Print how many UNIQUE visitors there are.
Sign in to practise
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Lab 6: Safe marks lookup
With marks = {"Mim": 78}, print Mim's marks AND Rafi's using .get() with default 0, expecting 78 0.
Sign in to practise
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
References & Further Reading
Related entries from PyLabs's own reference library.