The request

From Megan Collins, Head of Sales
Your goal
Set result to a list of the Monday-to-Friday counts (the first five values of daily_orders), in their original order.
- Store a list of 5 in
resultnot done yet
Your code
Loading the editor…
Reference solution
Mote checks your result
Press Run. Each row that matches the goal becomes a firefly in your lantern.
What Megan gets, and why
The lesson
A list holds values in order
A list is an ordered collection written with square brackets:
daily_orders = [42, 38, 51, 47, 60, 73, 68]
cart = ["Oak Side Table", "Linen Throw Pillow", "Brass Floor Lamp"]
Lists can hold any kind of value, and they keep the order you put things in.
Indexing starts at zero
You get a single item with its index in square brackets. The first item is index 0, not 1:
daily_orders[0] # 42 Monday
daily_orders[1] # 38 Tuesday
daily_orders[6] # 68 Sunday
daily_orders[7] # IndexError: list index out of range
Negative indexes count from the end, which is handy when you don’t know the length:
daily_orders[-1] # 68 the last item
daily_orders[-2] # 73 second to last
Slicing takes a range
A slice list[start:stop] returns a new list from start up to, but not including,
stop. Leave out either end to go to the edge:
daily_orders[0:5] # [42, 38, 51, 47, 60] Monday to Friday
daily_orders[:5] # same thing
daily_orders[5:] # [73, 68] the weekend
daily_orders[-3:] # [60, 73, 68] the last three days
daily_orders[::2] # [42, 51, 60, 68] every second day
The “up to but not including” rule feels odd at first, but it has a nice property: [:5] gives
exactly 5 items, and [:5] plus [5:] together are the whole list with nothing missing or
repeated.
Built-ins that work on lists
len(daily_orders) # 7 number of items
sum(daily_orders) # 379 total orders for the week
max(daily_orders) # 73 busiest day
min(daily_orders) # 38 quietest day
sum(daily_orders) / len(daily_orders) # 54.14... average per day
These are everyday analyst moves. “What was the busiest day?” is max; “which day was it?” is
daily_orders.index(max(daily_orders)), which returns 5, meaning Saturday.
Changing a list
Unlike strings, lists can be changed in place:
daily_orders.append(55) # add to the end
daily_orders[0] = 44 # correct Monday's figure
Be careful: append changes the list itself and returns None, so
daily_orders = daily_orders.append(55) leaves you with None instead of a list.
Lists can hold anything
A list doesn’t have to hold numbers. It can hold strings, booleans, or even other lists, and the
values don’t all need the same type, although in analysis work they almost always do. A list of
product names, a list of order IDs to check, a list of column names to keep: you’ll build lists like
these constantly, and every one of them supports the same indexing, slicing and len().
Common mistakes
- Off-by-one errors:
daily_orders[1:5]starts at Tuesday because index 1 is the second item. - Expecting
daily_orders[5]to be Friday. It’s Saturday; Friday is index 4. - Overwriting a list with the return value of
append,sortor other in-place methods.
If you know SQL
Slicing a list is a bit like LIMIT and OFFSET on rows that already have a fixed order. You’ll
see the same idea again in pandas with .head() and .iloc.
Lantern lit!
Practised: listsindexingslicinglen sum max
- Solved
- No hints
- First run
