Data structures · 10 sample answers

Python lists: practice questions with answers.

Lists are the first data structure most beginners use every day. This page checks whether you can read an index, change a list in place, and choose a method without guessing what it returns.

82 questions in the full bank · 21 choice · 11 fill · 50 output

What this topic tests

  • Read and slice a list without guessing at indexes.
  • Tell methods that change a list from methods that return a new one.
  • Choose a loop or a built-in when the result needs its own list.

Common mistakes

  • Using = to copy a list. Both names then point at the same list.
  • Removing items while iterating over that same list. Iterate over a copy instead.
  • Expecting .sort() to return a sorted list. It sorts in place and returns None.
Sample questions

Sample questions

10

Ten representative questions from the full bank. Try each one before opening the answer.

  1. Multiple choiceq0434

    1. Ming takes the last order from orders = ["Alice", "Bob", "Carol"]. What is orders[-1]?

    1. Alice
    2. Carol
    3. -1
    4. raises an error
    Show answer

    Answer

    B. Carol

    Why: Negative indexes count from the end, so -1 is the final element and -2 the one before it. Carol is returned, not the number -1.

  2. Multiple choiceq0446

    2. Which value cannot be used as a dictionary key?

    1. 1
    2. (1, 2)
    3. [1, 2]
    4. "a"
    Show answer

    Answer

    C. [1, 2]

    Why: Keys must be hashable, and a list is mutable so its hash could change; tuples, strings and ints are fine. A list key raises TypeError.

  3. Multiple choiceq0452

    3. 🧋 What is the difference between append and extend?

    1. there is no difference
    2. append adds one element; extend adds several
    3. extend adds one; append adds several
    4. append is faster
    Show answer

    Answer

    B. append adds one element; extend adds several

    Why: append takes a single object, so appending a list nests it; extend iterates its argument and adds each item. One element versus many.

  4. Multiple choiceq0470

    4. After b = a, what is the relationship between a and b?

    1. a copied list
    2. the same object
    3. a new tuple
    4. a deep copy
    Show answer

    Answer

    B. the same object

    Why: Assignment binds another name to the existing object; it never copies. Use copy() or a slice when you need two independent lists.

  5. Fill in the blankq0436

    5. Deduplicate repeat customers: how many elements are in `list(set(['milk','tea','milk']))`? (Enter a number.)

    Show answer

    Answer

    2

    Why: set keeps one copy of each distinct string, so milk and tea remain and the length is 2. The order is not guaranteed, so never rely on it.

  6. Fill in the blankq0453

    6. 🧋 Which method adds an element to the end of a list?

    Show answer

    Answer

    append

    Why: append is the one-argument method that grows the list at the end; insert needs an index and extend needs an iterable. Argument count is the giveaway.

  7. Fill in the blankq0474

    7. What is the common method for copying a list?

    Show answer

    Answer

    copy

    Why: list.copy() returns a shallow copy, the readable equivalent of a[:]; for nested structures use copy.deepcopy instead. The outer list is always new.

  8. Output predictionq0435

    8. What does this slice print?

    orders = ["Alice", "Bob", "Carol"]
    print(orders[1:])
    Show answer

    Answer

    ['Bob', 'Carol']

    Why: A slice from index 1 to the end keeps Bob and Carol and drops the first element. The start is included and the missing stop means the end.

  9. Output predictionq0475

    9. What does `annotate([])` return?

    def annotate(garden):
        if not garden:
            return []
        verify_board(garden)
        row_len = len(garden[0])
        col_len = len(garden)
        board = [list(row) for row in garden]
    
        for index1 in range(col_len):
            for index2 in range(row_len):
                if board[index1][index2] != ' ':
                    continue
    
                low = max(index2 - 1, 0)
                high = min(index2 + 2, row_len + 2)
                counts = garden[index1][low:high].count('*')
    
                if index1 > 0:
                    counts += garden[index1 - 1][low:high].count('*')
                if index1 < col_len - 1:
                    counts += garden[index1 + 1][low:high].count('*')
                if counts == 0:
                    continue
    
                board[index1][index2] = str(counts)
        return [''.join(row) for row in board]
    
    
    def verify_board(garden):
        # Rows with different lengths
        row_len = len(garden[0])
        if not all(len(row) == row_len for row in garden):
            raise ValueError('The board is invalid with current input.')
    
        # Unknown character in board
        character_set = set()
        for row in garden:
            character_set.update(row)
        if character_set - set(' *'):
            raise ValueError('The board is invalid with current input.')
    
    print(annotate([]))
    Show answer

    Answer

    []

    Why: The guard returns [] before verify_board runs, so an empty garden never reaches the counting loops. There are no cells, hence no annotation.

  10. Output predictionq0492

    10. What does `spiral_matrix(5)` return?

    def spiral_matrix(size):
        matrix = [[0]*size for row in range(size)]
        idx = 0
        jdx = -1
        element = 1
    
        digital = [0, 1, 0, -1]
        disco = [1, 0, -1, 0]
    
        for edx in range(2*size - 1):
            for _ in range((2*size - edx) // 2):
                idx += digital[edx % 4]
                jdx += disco[edx % 4]
                matrix[idx][jdx] = element
                element += 1
        return matrix
    
    print(spiral_matrix(5))
    Show answer

    Answer

    [[1, 2, 3, 4, 5], [16, 17, 18, 19, 6], [15, 24, 25, 20, 7], [14, 23, 22, 21, 8], [13, 12, 11, 10, 9]]

    Why: A 5×5 spiral is an outer ring of 16 cells (1 to 16) around a 3×3 spiral (17 to 25), so the centre cell ends up holding 25.

Common questions

How many list questions are in the full bank?

The full bank has more than 80 questions tied to the Lists lesson, with a mix of multiple choice, fill in the blank, and output prediction.

Should I use a list or a tuple?

Use a list when the collection should change. Use a tuple when the shape and order are fixed, or when you need a hashable key.

Keep the streak going.

The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.

Open full practice