A set is a Python collection of distinct, unordered, hashable elements. Use it when a program needs uniqueness or efficient membership lookup, such as filenames, email addresses, postal codes, or IP addresses. section 4.2
Choose the operation that matches the data-cleaning or comparison task:
- Create a set from a sequence with set(values), or add items individually with add and in groups with update. section 4.19
- Remove duplicates automatically; a set retains each value only once. section 4.19
- Combine dictionary keys with union (|), then iterate once over the resulting unique keys. section 4.14
- Remember that set elements must be hashable, so a list cannot be inserted as one set element. section 4.19
Count unique words while reading lines
unique_words = set() for line in lines: unique_words.update(line.split()) unique_count = len(unique_words) The set collects words from every line without repeated entries, and len reports the number of distinct words. section 5.12
all_keys = first.keys() | second.keys()
Inspect the concise dictionary-key union used to gather every distinct key from two dictionaries. section 4.14














