Python, properly · Chapter 8 of 42

Lists

By the end of this chapter you can keep many values in one list, pick and slice items, add and remove them, sort them, turn text into a list and back, and copy a list without falling into Python's most famous trap. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: First steps
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.

Why you need this

A menu is not one dish, it is many, and DabbaDrop's day is full of "many": today's dishes, a customer's basket, the prices of an order, the riders on shift. Bhavna types today's menu on one line; Rafiq wants prices sorted; Farah wants a basket that grows as the customer adds things.

One name per value does not scale to a basket of six items. A list holds any number of values, in order, under one name, and it can grow and change as the program runs.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    todays_menu.py: first, last and the middle

    Bhavna types today's dishes on one line, separated by a comma and a space. Ask Dishes? (a space after the question mark), turn the answer into a list, and print how many dishes there are, the first, the last, and the ones in between joined with a space, a vertical bar and a space. For Dal Tadka, Roti, Chole, Jeera Rice the check sees:

    Dishes? 4 dishes

    First: Dal Tadka

    Last: Jeera Rice

    Middle: Roti | Chole

    The check always types at least 3 dishes.

  2. Task 2

    prices.py: sorted, top 3, and the original order

    Rafiq types a few prices separated by spaces. Ask Prices? (a space after the question mark), make a list of whole numbers from the answer, and print it sorted from highest to lowest, the top 3, and finally the list in the order it was typed. For 140 90 260 40 220 the check sees:

    Prices? Highest first: [260, 220, 140, 90, 40]

    Top 3: [260, 220, 140]

    As typed: [140, 90, 260, 40, 220]

    With fewer than 3 prices, Top 3 shows all of them.

  3. Task 3

    basket.py: append or extend

    Build a customer's basket as one flat list. Ask Main dish? and then Add-ons? (each with a space after the question mark). The add-ons are separated by a comma and a space, or the answer is empty for none. The basket is the main dish, then each add-on as its own item, then a free Gulab Jamun at the end. Print the list, then how many items it has. For Chole and Roti, Roti, Jeera Rice the check sees:

    Main dish? Add-ons? ['Chole', 'Roti', 'Roti', 'Jeera Rice', 'Gulab Jamun']

    5 items

    For Chole and no add-ons:

    Main dish? Add-ons? ['Chole', 'Gulab Jamun']

    2 items

  4. Task 4 · interview question

    Interview: tomorrow.py, a copy, not a second name

    Bhavna plans tomorrow's menu from today's by swapping one dish. Ask Today's menu? (dishes separated by a comma and a space), then Swap out? and Swap in? (each prompt ends with a space). Tomorrow's menu is a copy of today's with that one dish replaced, in the same position; today's menu must not change. Print both lists. For Chole, Roti, Curd Rice, swapping Roti for Butter Naan, the check sees:

    Today's menu? Swap out? Swap in? Today: ['Chole', 'Roti', 'Curd Rice']

    Tomorrow: ['Chole', 'Butter Naan', 'Curd Rice']

    If the dish to swap out is not on the menu, print only Not on today's menu: and the dish.

What you will learn

  • Lists: making them, indexing and slicing (as with strings).
  • Changing a list: item assignment, append, extend, insert, remove, pop.
  • Text and lists: split and join, and building a list in a loop.
  • Sorting: sorted() against .sort(), plus min, max, sum.
  • The copying trap: two names on one list, and .copy().

More in First steps

Build "Lists" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.