Recommended
Python Data Structures — Timed Test (3 questions)
No account needed. Answers and explanations arrive when you submit.
Python Data Structures — the theory
Python provides several built-in data structures that are fundamental to writing effective code, each suited to different kinds of problems.
Lists. A list is an ordered, mutable (changeable) collection of items, created with square brackets, like [1, 2, 3]. Lists can contain items of different types, can grow or shrink after creation (adding items with .append(), removing with .remove() or .pop()), and support indexing to access individual items by position, including negative indexing to count from the end. Lists are one of the most commonly used data structures in Python, suited to situations where you need an ordered collection that might change over time.
Tuples. A tuple is similar to a list — an ordered collection — but immutable, meaning once created, its contents cannot be changed. Tuples are created with parentheses, like (1, 2, 3). Because they're immutable, tuples are often used for fixed collections of related values that shouldn't change, and they can be used in contexts (like dictionary keys) where a list, being mutable, cannot.
Dictionaries. A dictionary is a collection of key-value pairs, providing fast lookup of a value given its associated key, created with curly braces, like {"name": "Alice", "age": 30}. Dictionaries are unordered in older Python versions but maintain insertion order in modern Python. They're well suited to situations where you need to look up values by a meaningful identifier rather than a numeric position — for example, storing configuration settings by name, or counting occurrences of items by using each item as a key.
Sets. A set is an unordered collection of unique items, created with curly braces or the set() function. Sets automatically eliminate duplicate values and support efficient membership testing (checking whether an item is present) as well as mathematical set operations like union, intersection, and difference. Sets are useful whenever you need to track a collection of distinct items and don't care about their order, or when you need to quickly deduplicate a collection.
Choosing the right structure. Picking the appropriate data structure for a task has real practical consequences: using a list when you need fast membership testing on a large collection is much slower than using a set for that same purpose, and using a dictionary when values should be accessed positionally rather than by a meaningful key adds unnecessary complexity. Understanding the strengths of each structure — ordered versus unordered, mutable versus immutable, indexed versus key-based — is central to writing efficient, readable Python.
Nesting and combining structures. These structures can be nested and combined — a list of dictionaries, a dictionary whose values are lists, and so on — which is extremely common in real code for representing more complex data, like a list of user records where each record is a dictionary of that user's fields.
Common operations across structures. Python provides consistent ways to work with these structures: iterating over their contents with a for loop, checking membership with the in keyword, and finding their length with the built-in len() function — a consistency that makes it relatively straightforward to reason about code even across different structure types once you understand these shared patterns.
Comprehensions. Python offers a compact, idiomatic syntax for building these structures from existing sequences: a list comprehension like [x * 2 for x in numbers] creates a new list by transforming each item, optionally filtering with a condition like if x > 0; the same pattern exists for dictionaries and sets. Comprehensions replace many short loops with a single readable line, and they are so common in real-world Python that reading them fluently is effectively required — though deeply nested comprehensions can become harder to read than the loops they replace, at which point an ordinary loop is the better choice.
Copying versus referencing. Because lists and dictionaries are mutable, assigning one to a new variable does not copy it — both names now refer to the same underlying object, and a change through either name is visible through both. This aliasing behavior is one of the most common sources of confusing bugs for newcomers. When an independent copy is genuinely needed, Python provides explicit ways to make one, including a distinction between a shallow copy (copying the container but sharing nested contents) and a deep copy (copying everything recursively).
A practical sense of performance. The structures differ in what they make fast: lists are quick for adding at the end and reading by position, but slow for searching a large collection item by item; dictionaries and sets make lookup by key or membership testing fast even at large sizes; tuples behave like lists for reading but their immutability makes intent clearer and allows uses lists can't serve. These differences barely matter at small sizes and dominate at large ones — which is why structure choice is a correctness-of-scale decision, not just a style preference.
Mastering these built-in structures — knowing not just their syntax but when each one is the right tool for a given problem — is one of the most practically valuable skills in everyday Python programming, well beyond just knowing the language's basic syntax.
Sample questions
Three questions from this topic, with the answer and the reasoning shown.
Q1EasyWhat is a dictionary in Python best suited for?
- Looking up values by a meaningful key rather than a numeric positionCorrect
- Storing only a single value with no structure
- Guaranteeing duplicate keys are allowed
- Performing mathematical calculations directly
Explanation
Dictionaries provide fast lookup of values by a meaningful key, unlike lists which are accessed by numeric position.
Q2EasyWhat is a key difference between a Python list and a tuple?
- A list is mutable, while a tuple is immutableCorrect
- A tuple can hold more items than a list
- A list cannot contain numbers
- A tuple must always be empty
Explanation
Lists are mutable and can be changed after creation, while tuples are immutable once created.
Q3MediumWhat distinguishes a Python set from a list?
- A set automatically eliminates duplicate values and is unorderedCorrect
- A set preserves duplicates and strict order
- A set cannot hold more than one item
- A set requires all items to be the same length
Explanation
Sets automatically eliminate duplicate values and are unordered, unlike lists which preserve order and allow duplicates.