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?