# Python data structures for analytics: lists, tuples, dictionaries and sets Choose lists, tuples, dictionaries or sets by access pattern, then validate empty collections, duplicate identifiers and mixed types before DataFrame use. - Canonical: https://satracking.net/guides/python-data-structures-lists-tuples-dictionaries-sets/ - Language: en - Category: platforms - Level: intermediate - Published: 2026-11-14 - Updated: 2026-11-14 --- ## Purpose Choose lists, tuples, dictionaries or sets by access pattern, then validate empty collections, duplicate identifiers and mixed types before DataFrame use. The *Python For Data Science Cheat Sheet* introduces core structures including lists and their selection and slicing behavior. This guide organizes those foundations around common data-analysis tasks. ## 1. Choose a structure based on the job - **List:** an ordered, mutable sequence. Use it for collections that may grow, shrink or be updated. - **Tuple:** an ordered, immutable sequence. Use it for fixed records or values that should not be reassigned. - **Dictionary:** key-value mapping. Use it to associate a field name or identifier with a value. - **Set:** collection of unique values. Use it for membership checks and deduplication where order is not the requirement. A structure is not automatically a table. A list of dictionaries can represent records, but it does not provide the full indexing, typing and vectorized operations of a dataframe library. ## 2. Index and slice deliberately Python sequence indexes start at zero. For `values = ["north", "south", "west", "east"]`, `values[0]` returns the first item and `values[-1]` the last. A slice such as `values[1:3]` returns items at positions 1 and 2; the end boundary is excluded. Use slices to select a range without changing the original sequence. Check boundaries explicitly: an index outside the sequence raises an `IndexError`, while a slice that extends beyond the available range is safely clipped. ## 3. Understand mutability Lists can be changed in place: append, remove, replace or reorder elements. Tuples cannot be changed after creation. This distinction matters when multiple parts of a program share a reference to the same object. A frequent source of defects is modifying a list while iterating over it. Prefer building a new filtered list or iterating over a copy when the collection must change. ## 4. Use dictionaries for named fields A dictionary makes a record easier to read than a positional list: ```python record = {"campaign": "spring", "sessions": 1200, "conversions": 48} rate = record["conversions"] / record["sessions"] ``` Use `.get()` when a key may be absent and define what missing means. Do not silently replace missing business values with zero unless zero is genuinely the correct interpretation. ## 5. Use sets for uniqueness and membership A set removes duplicate values and supports efficient membership checks: ```python seen_ids = set() if event_id not in seen_ids: seen_ids.add(event_id) ``` Sets are useful for comparing identifiers between two extracts. They do not preserve the analytical ordering of a sequence, so do not use them when row order is meaningful. ## 6. Convert structures carefully A list of dictionaries can be converted into a pandas DataFrame, but inspect the resulting columns, missing values and types. Records with inconsistent keys may create missing cells. A dictionary of lists requires compatible lengths when constructing a rectangular table. Before transformation, document the grain of each record: one event, one user, one order or one product. A technically valid structure can still represent the wrong analytical grain. ## 7. Practical validation Test empty collections, duplicate identifiers, missing dictionary keys, mixed data types and boundary indexes. Add assertions for assumptions that must always hold, such as uniqueness of an event identifier or non-negative session counts. The core lesson is to choose the simplest structure that matches the operation. Lists preserve ordered collections, tuples protect fixed sequences, dictionaries express named relationships and sets support uniqueness and membership. ## Official references - Python Cheat Sheet (source collection) — https://www.datacamp.com/cheat-sheet/python-for-data-science-cheat-sheet (verified 2026-11-14)