Gru stands before a chaotic crowd of Minions.
Some are juggling chainsaws.
Some are chewing wires.
Some are trying to stack bananas into a life-sized statue of Kevin.
It is… not going well.
“I need order!! I need organization!! And most importantly… I need minions who can follow instructions!!”
You — yes, YOU, his newest Junior Evil Programmer™ — have been summoned to clean up the digital mess behind the scenes: the lists that track minions, their skills, their training status, and their general chaos levels.
But while loops behave differently than for loops… And Gru needs to modify these lists as you’re looping through them.
In past chapters, you’ve used for loops to go through lists — which is perfect when you’re just looking at items.But Gru wants something more powerful:
Remove minions while looping
Move minions to other lists
Recruit only certain minions
Clean up duplicates
Stop when a certain condition is no longer true
A for loop can’t adapt when the list changes — but a while loop can.
With a while loop, you can loop as long as something is true
The loop continues until the condition becomes false, which gives you tremendous flexibility.
You should try this sample code. This will move items from 1 list to another. It wasn't really possible using a for loop due to the way that lists are processed!!
REMEMBER: As one list shrinks, the list other list grows
We're going to jump right into coding this, even before explaining. Hopefully, you'll be able to see what's going on, as we've seen all of this stuff before!! This should help you practice pop and list copying!!
Gru only wants to recruit minions with names longer than 3 characters because short names are "too suspicious."
Rules:
Start with a list of training minions.
Use a while loop to only move minions with names longer than 3 characters to the recruited list.
Print a message for each recruited minion.
Print a message for any minion tossed from being recruited
At the end, print the list of recruited minions and those left untrained.
Each minion has a skill level (as a number). Gru only wants to recruit minions with skill levels of 5 or higher.
Rules:
Start with two lists: one for minion names and one for their skill levels.
Use a while loop to recruit only skilled minions.
Print a message for each recruited minion, including their skill level.
At the end, print the names and skill levels of all recruited minions.
This code removes all instances of 'gru' from the list of characters
We start with a list containing multiple instances of 'gru'.
After printing the list, Python enters the while loop because it finds the value 'gru' in the list at least once.
Once inside the loop, Python removes the first instance of 'gru', returns to the while line, and then reenters the loop when it finds that 'gru' is still in the list.
Instead of copying things over, sometimes we want to remove specific things from a list. Maybe we have a value which has been spammed into a list multiple times, or we just want unique items!
We used that to take a specific value away by:
my_list.remove('the_thing_to_remove')
What if we want to remove EVERY INSTANCE of something??
USE A WHILE LOOP!!! IT KEEPS RUNNING UNTIL THAT CONDITION IS TRUE!!!
Gru is in full preparation mode for his next big plan to outshine every villain on Earth. But there’s a problem—his roster of minions is a complete mess! His assistants have been accidentally duplicating entries in their lists, and now the lab, garage, and storage room records are overrun with the same names appearing again and again. It’s chaos! Gru has personally requested your help as his top coder.
You must step in, clean up these lists, and ensure no minion’s name appears more than once in any of the rosters. Every area must have a unique, organized list of minions before Gru can finalize his plan. Do you have what it takes to bring order to this banana-fueled madness?
✔ STEP 1 — Create the Lists
Start with these lists:
✔ STEP 2 — Remove Duplicates Using While Loops
For each list, do the following:
Create a new empty list
Loop through the old list
Only add a name to the new list if it isn’t already in it
Count how many duplicates were removed
This MUST use a while loop.
✔ STEP 3 — Print Cleaned Rosters
Once you’ve cleaned up all the rosters, print the results for Gru. Each area should have its own unique list of minions. Make sure your output is neat and organized so Gru can quickly review the updated records.
✔ STEP 4 (Advanced Required): Combine & Clean
After cleaning each list:
Combine them into a master roster
Remove duplicates again
Sort alphabetically
Print the final list
Count how many total Kevins existed across all three lists before cleaning.
Assign each minion a random skill rating (1–10) and only keep skill ≥ 4.
For each list:
Print how many minions were originally in the list
Print how many remained
Print the “cleaning efficiency” percentage