Victor Jeman Academy
Python Fundamentals10 min

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 for insert(), min(), and max() 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() and max() 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:

text
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:

text
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:

text
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:

text
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:

text
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

After the hero defeats a monster, they find 3 items: "Fire Sword", "Rusty Armor", "Magic Ring". Display the found items with numbers. Ask the player to choose which item to keep (1, 2, or 3). Add the chosen item to the hero's inventory and display the updated inventory. If the player enters an invalid number, display an error message.

Exercise : Equipment system

The hero has an inventory with items and stat variables (attack, defense). Create the inventory with: "Fire Sword", "Iron Shield", "HP Potion", "Magic Ring". Display the inventory with numbers. Ask the player to choose an item to equip. If they choose "Fire Sword", add +10 to attack. If they choose "Iron Shield", add +7 to defense. Remove the equipped item from the inventory with pop() and display the updated stats and the remaining inventory.

Exercise : Shopping list manager

Write a program that asks the user to enter products, one at a time. After each product is added, display how many products the list has. When the user types "stop", the program stops and displays the complete list.

Hint: use a while True loop and break when the user types "stop".

Exercise : Playlist manager with favorites

Create two lists: playlist with 5 songs and favorites empty. Traverse the playlist with a while loop and for each song ask the user if it is a favorite (yes/no). If yes, add it to the favorites list. At the end, display the favorites list and how many songs are in it.

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.

Question 1 of 4

What does insert() do that append() does not?