Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

Python data structures help programmers organize, store, and process information efficiently. This cheat sheet covers the most common built-in structures: lists, tuples, dictionaries, and sets. Students need these tools to write cleaner programs, solve problems faster, and understand how real Python code manages collections of data.

It also highlights comprehension syntax, which is a compact way to build new collections from existing ones.

The core ideas include choosing the right structure for the job, using indexing and keys correctly, and understanding which structures can be changed after creation. Lists and tuples store ordered items, dictionaries store key-value pairs, and sets store unique items. Comprehensions follow patterns like [expression for item in iterable if condition] and can create lists, sets, and dictionaries.

Knowing these patterns makes loops shorter while keeping code readable.

Key Facts

  • A list is ordered and mutable, so nums = [3, 5, 7] can be changed with nums[0] = 10.
  • A tuple is ordered and immutable, so point = (4, 2) cannot have its individual elements reassigned.
  • A dictionary stores key-value pairs, so ages = {'Mia': 15} uses ages['Mia'] to access the value 15.
  • A set stores unique unordered values, so set([1, 1, 2, 3]) becomes {1, 2, 3}.
  • Python indexing starts at 0, so items[0] is the first element and items[-1] is the last element.
  • A list comprehension has the form [expression for item in iterable if condition], such as [x * 2 for x in nums if x > 0].
  • A dictionary comprehension has the form {key_expr: value_expr for item in iterable if condition}, such as {x: x * x for x in range(5)}.
  • Use len(collection) to find the number of items in a list, tuple, dictionary, set, or string.

Vocabulary

List
A mutable ordered collection that can store multiple values in a single variable.
Tuple
An immutable ordered collection often used for fixed groups of related values.
Dictionary
A mutable collection that maps unique keys to values for fast lookup.
Set
A mutable collection of unique values with no guaranteed order.
Comprehension
A compact Python expression used to create a new collection from an iterable.
Iterable
An object that can be looped over, such as a list, tuple, string, dictionary, set, or range.

Common Mistakes to Avoid

  • Using index 1 for the first list item is wrong because Python indexing starts at 0, so the first item is items[0].
  • Trying to change a tuple item is wrong because tuples are immutable, so use a list if the values need to change.
  • Looking up a dictionary value with the wrong key is wrong because dictionary access depends on exact key matches, including spelling and capitalization.
  • Expecting a set to keep duplicate values is wrong because sets automatically remove duplicates.
  • Writing a comprehension that is too complex is a mistake because short readable loops are better than confusing one-line code.

Practice Questions

  1. 1 Given nums = [2, 4, 6, 8], what are nums[0], nums[-1], and len(nums)?
  2. 2 Write a list comprehension that creates the list [1, 4, 9, 16, 25] from range(1, 6).
  3. 3 Given scores = {'Ana': 92, 'Ben': 85, 'Chris': 90}, what Python expression returns Ben's score?
  4. 4 A program needs to store each student's name with a current grade and quickly look up a grade by name. Which data structure should be used, and why?

Understanding Python Data Structures & Comprehensions

A structure is more than a container. Its behavior affects the rest of a program. Think about a school grade tracker.

A list works well when the program must keep test scores in the order they were entered. A dictionary works better when each score belongs to a student name or student ID. A set is useful for recording which clubs a student joined because repeated entries should not create duplicate club names.

Programs often combine structures. A dictionary might connect each student to a list of scores. This kind of nesting appears in games, websites, scientific data, and school projects.

Changing data needs care. When one variable is assigned to an existing list, both variables can refer to the same list in memory. Changing the list through one variable then changes what the other variable sees.

This surprises many beginners. Making a copy creates a separate list when that is what the program needs. Methods such as append, remove, pop, sort, and reverse may change a list directly.

Other operations create a new result instead. Reading the documentation or testing a small example helps students tell the difference. This matters when a program must preserve original data for later comparison.

Dictionary work requires attention to missing information. Trying to get a value with a key that is not present can stop a program with an error. The get method can provide a safe fallback value when data may be incomplete.

For example, an attendance program may not have a record for every student yet. Dictionary keys need to be stable values, such as strings, integers, or tuples made from stable values. A list cannot be used as a dictionary key because its contents can change.

Sets follow a related rule because their members must be values Python can track reliably. These limits help Python find data quickly.

Comprehensions are useful when a program transforms or filters a collection in one clear step. A list of passing scores can be made from all scores by keeping only values at or above the chosen mark. A dictionary comprehension can turn a list of names into a lookup table.

The compact form is helpful only while it stays readable. If the expression has several conditions, nested loops, or complicated calculations, a normal loop is often easier to debug. Students should trace a comprehension one item at a time.

Identify the source collection, the condition, and the value being produced. This habit makes compact Python code much less mysterious.