Python/Collections 7 min

Sets and dictionaries

Membership tests and key-value lookups.

dictsetcounting

A set holds unique, unordered values and tests membership in constant time. It also supports mathematical operations.

python · editable
loading editor…

A dict maps keys to values. Use .get for a safe lookup with a default, and .items() to loop over pairs.

python · editable
loading editor…

Counting occurrences is the classic dict pattern, and a dict comprehension builds one in a single line.

python · editable
loading editor…