Skip to contentSkip to navigation

Module 4

Dictionaries and Sets

Map key-value relationships, model unique collections, and apply fast set algebra.

// Lessons

6

// Quiz questions

90

// Reading time

105 min

// Exam length

15 Q

// Coding exercises

10

  1. 1Set Algebra & Venn OperationsCompute mathematical set unions, intersections, symmetric differences, and subset tests. 15 min 15 questions
  2. 2Dictionaries & Hash MapsStore associative key-value data with fast O(1) hash lookups and key immutability constraints. 15 min 15 questions
  3. 3Dictionary Methods & Key MergingPerform safe lookups with .get(), group data with .setdefault(), and merge maps with the pipe operator. 15 min 15 questions
  4. 4Set Creation & DeduplicationEliminate duplicate items, enforce element uniqueness, and achieve fast O(1) membership checks. 20 min 15 questions
  5. 5Collections Module ToolsCount frequencies with Counter, eliminate missing-key errors with defaultdict, and use namedtuples. 20 min 15 questions
  6. 6Nested Data StructuresNavigate and construct multi-layered JSON trees, nested lists, and deep copying workflows. 20 min 15 questions

Coding practice: write it yourself

Real Python, real checks: write your answer, press Run checks, and keep going until it passes. (4 exercises)

All module exercises

Coding practice needs an account. Sign in or create a free ID to run these exercises.

Lab 1: Config lookup

Sign-in required

Given config = {"host": "localhost", "port": 5432}, print Server running on localhost:5432 using values from the dictionary.

Coding Practice is Locked

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.

Lab 2: Update fruit stock

Sign-in required

Given stock = {"apple": 10, "orange": 5}, add 7 more apples and add "banana": 12. Print: {'apple': 17, 'orange': 5, 'banana': 12}.

Coding Practice is Locked

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.

Lab 3: Shared interests

Sign-in required

Find common items between a = {"python", "sql", "react"} and b = {"sql", "docker", "python"}. Print the sorted list: ['python', 'sql'].

Coding Practice is Locked

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.

Lab 4: Letter frequency

Sign-in required

Count occurrences of each character in "mississippi" using a dictionary. Print the count of 's': s: 4.

Coding Practice is Locked

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.

References & Further Reading

Related entries from PyLabs's own reference library.

  • get() (Look up a value safely without raising a KeyError if missing.)
  • items() (Return a dynamic view of all (key, value) pairs in the dictionary.)
  • union() (Return a new set with elements from this set and all others.)