Lists, part 2: more list tools
Place items anywhere in a list with insert(), and find the smallest and largest value with min() and max().
What You'll Learn
- Insert an element at any position in a list with insert()
- Find the smallest and largest value of an iterable with min() and max()
- Reinforce list mutation and while-traversal through inventory and playlist exercises
In part 1 you grew a list with append() (always at the end) and shrank it with pop(). That covers most of what you do with a list day to day. But two questions come up the moment your lists get real: what if I need an item somewhere in the middle, not at the end? And what if I just want the smallest or largest value without walking the whole thing by hand? This part answers both.
We learn these tools here on small, everyday lists, a school schedule, a row of grades, a playlist, because they read at a glance. Then, in the exercises, you take the same tools back to the dungeon: insert loot into an inventory, find the highest stat, manage what the hero carries.
[!TIP] Optional deep-dive This is a "part 2" of lists. If you just want the basics, you already have everything you need from the previous lesson (
append(),pop(), indexing,len()). Come back forinsert(),min(), andmax()when you want them.
In this lesson, you will be able to:
- Place an element at any position in a list with the
insert()method - Find the smallest and largest value with the
min()andmax()functions - Reuse those tools on the hero's inventory and a playlist
The insert() method
append() only ever drops an item at the end of the list. Sometimes that is not where it belongs. Say you have a school schedule and the principal decides a new period should land in the second slot, not after everything else. For that you use insert().
insert() takes two things: the position you want, and the value to put there. Everything from that position onward slides one step to the right to make room.
Result:
Original schedule: ['Mathematics', 'Physics', 'English', 'History']
Updated schedule: ['Mathematics', 'Assembly', 'Physics', 'English', 'History']
insert(1, "Assembly") reads as "put Assembly at index 1". "Physics" was at index 1, so it (and everything after it) shifts right by one. Nothing gets overwritten; the list just gets longer.
To put something at the very front, you insert at index 0:
Result:
Thursday schedule: ['PE', 'Chemistry', 'Biology', 'History', 'Geography']
So you now have three ways to grow a list: append() for the end, insert(0, ...) for the front, and insert(index, ...) for anywhere in between.
The min() and max() functions
Sometimes you do not care about every value in a collection, just the extremes: the lowest grade, the highest score, the cheapest item. Python hands you min() and max() for exactly that. You pass an iterable (a list, for now) and you get back the smallest or largest value, no loop required.
Result:
Math grades: [7, 9, 5, 10, 8, 6]
Lowest grade: 5
Highest grade: 10
One line each, and you never touched an index. Compare that to the while traversal from part 1, where you would track a "best so far" variable by hand. min() and max() do that bookkeeping for you.
These functions are not list-only. They work on any iterable. On a list of strings, "smallest" and "largest" mean alphabetical order:
Result:
First subject alphabetically: Art
Last subject alphabetically: Physics
You can also pass values directly, separated by commas, instead of a single list. Handy when you have a few loose variables and just want the bigger or smaller one:
Result:
Best test grade: 9
Worst test grade: 6
insert(index, value) places an element at any position and shifts the rest right. min() and max() return the smallest and largest value of an iterable (a list of numbers, a list of strings, or loose values), with no loop to write yourself.
Exercise : Loot after battle
Exercise : Equipment system
Exercise : Shopping list manager
Exercise : Playlist manager with favorites
We did not open the game file this lesson, and that is on purpose. insert(), min(), and max() are tools going into your kit, ready for when you reach for them inside the game later. The kit grows; the game waits, exactly where you left it.
You have spent two parts treating a list as a sequence of positions you reach into with [0], [-1], and an index that moves. Next lesson you point those same moves at text: a string is just a sequence of characters, so you can slice a username apart, grab the first letter, or read the last one with the indexing you already know. (And a few lessons on, for will walk a list for you, no counter to start, check, or forget to increment.)
Test Your Knowledge
Check how well you understood the lesson with these 4 questions.

