🗂️ Sets & Dicts

Sets & Dicts: A set is like a fingerprint scanner at a door — even if the same person tries to enter twice, there's only ONE record inside.

A set is like a fingerprint scanner at a door — even if the same person tries to enter twice, there's only ONE record inside. Writing list(bug_ids) just copies the list, duplicates stay; writing set(bug_ids) makes Python check "have I seen this before?" for each item and dedupe automatically. The cost? Set items have NO order and can't be indexed (set[0] fails) — because a set uses a hash table for speed, not an ordered list. In Java, this maps to HashSet. A practical QA use case: after running 1000 tests, you can answer "how many DISTINCT error messages came up?" in a single line with len(set(error_messages)). So if a set is this useful, why not use one everywhere instead of a list? Because the price is not only the lost ordering: a set accepts only IMMUTABLE (hashable) items — put a list or a dict in one and you get a TypeError. Java shows you the same rule from another angle: drop a mutable object into a HashSet, mutate it, and the object effectively disappears. The rule of thumb: reach for a set where uniqueness and fast "is it in there?" matter, and for a list where the order itself carries meaning (the execution order of your tests, for instance).

Python set is Java's HashSet. No import needed. Created with {1, 2, 3}. No guaranteed order.

Micro Lab: Python coding practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Step by Step: Python coding practice

Read inputs and data types

Complete the critical line with the smallest change

Run the code and compare expected output

If it fails, read traceback or assertion message

Make the result reusable for a test report

What is the safe order for running Python QA code?

What happens when you add the same element to a set twice?

The duplicate is silently ignored

Sets are unique. Adding a duplicate is silently ignored — no error, no duplicate.