Skip to content
PrepMint

Python

Python Data Structures

Lists, dicts, sets, tuples

3 questions
Easy· 2Medium· 1

Last reviewed

Recommended

Python Data Structures — Timed Test (3 questions)

TimedEasy3 questions · 3 min
Start test

No account needed. Answers and explanations arrive when you submit.

What this topic tests

The mix every Python Data Structures set is built to, and the questions published against it so far. Nothing here is hidden before you start.

Python Data Structures — target difficulty mix and published question count per level
LevelTarget sharePublished
Easy40%2
Medium40%1
Hard20%0
Total3

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

A dictionary answers "what is the value stored under this name?" without searching for it. The tutorial draws the contrast directly: "Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type". The glossary calls it "An associative array, where arbitrary keys are mapped to values." Swap a list plus a linear scan for a dictionary lookup and the scan disappears — the cost of finding a user by id stops growing with the number of users.

Two details will save you an afternoon. Keys must be hashable, since "A mapping object maps hashable values to arbitrary objects" — a string, a number or a tuple qualifies, a list does not. And one key holds exactly one value: assigning to a key that already exists overwrites it silently, which is how a dictionary built inside a loop quietly loses earlier entries.

The other options miss what the type is for. A plain variable stores a single unstructured value, keys are unique rather than duplicated, and arithmetic belongs to operators and modules, not to the container.

Open this question on its own page

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

Both types hold an ordered sequence and accept any kind of item, so the difference that matters is what you may do to them afterwards. The reference says: "Lists are mutable sequences, typically used to store collections of homogeneous items". The tutorial gives the other half: "It is not possible to assign to the individual items of a tuple, however it is possible to create tuples which contain mutable objects, such as lists."

That property settles two decisions you will face. Dictionary keys and set members must be hashable, and "Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally" — so a coordinate pair works as a key when it is a tuple and raises TypeError as a list. A tuple passed to another function also cannot be edited behind your back, which is why a mutable default argument is a classic Python bug and a tuple is not.

The other options invent rules Python does not have: no size ceiling separates them, lists hold numbers constantly, and a tuple may carry any number of items.

Open this question on its own page

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

A set gives up two guarantees a list makes, and buys something a list cannot offer. The tutorial defines it as "A set is an unordered collection with no duplicate elements." and the library reference as "A set object is an unordered collection of distinct hashable objects." Storing by hash is what makes that trade pay: asking whether an item is in a list walks it element by element, while the same question asked of a set is a single lookup, however large the set has grown.

So the working rule is concrete. If your loop is asking "have I seen this one already?", or you are collapsing a sequence down to its unique values — "Using set() on a sequence eliminates duplicate elements." — reach for a set. Keep the list when position or repeat counts carry meaning, because "Because sets are unordered, iterating over them or printing them can produce the elements in a different order than you expect."

The wrong answers describe a list instead of a set, or impose a limit no Python collection has.

Open this question on its own page

More Python topics

All of Python