Skip to the content

Choosing the structure

Python · Unit 24: Data structures

You already have seven tools, and each one answers a different question. Three of them you've known for a while.

List: what's in this position, and in what order? Dictionary: what belongs to this name? Set: have I seen it already?

prices = {"bread": 12}
seen = {"bread", "milk"}
print(prices["bread"])
print("milk" in seen)

Prints

12
True

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Multiple choice

    In a raffle you write down the numbers that already came out, and the only thing you ever ask is whether a number is already out. What do you use?

  2. 2. Find the bug

    It should say how many orders were written down, that is 4. Which line has the error?

  3. 3. Predict the output

    The same numbers go into both. What does it print?

  4. 4. Multiple choice

    At the diner you write down the orders and cook them in the order they arrived. You want to take out the first one without moving all the others. What do you use?

  5. 5. Find the case that fails

    no_repeats(nums) should return the different numbers in the order they showed up, or [] if the list comes in empty. Which call breaks it?

  6. 6. Multiple choice

    A board with 500 notices, each one with its urgency number. New notices keep coming in and you want to take out the most urgent one many times, without walking all 500 each time. What do you use?

  7. 7. Predict the output

    The map says which stations each one reaches in a single hop. What does it print?

  8. 8. Complete the code

    What's pending is handled in the order it was written down. It should print A, B and C.

  9. 9. Multiple choice

    Why is seen a set and not a list?

  10. 10. Put the lines in order

    Put least_sold(sales) together; it takes a dictionary from product to sales and returns the one that sold least.

  11. 11. Predict the output

    The same three numbers, kept two different ways. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →