Skip to contentSkip to navigation

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

  1. 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
  2. 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
  3. 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
  4. 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)

All module exercises

The Lab 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.

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

Sign-in required

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

Sign-in required

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

Sign-in required

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

Sign-in required

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

Sign-in required

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.

  • 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.)