I strongly believe that if you can understand laundry, you can understand Algorithms and multi-level iteration.
Now, I must point out that laundry is one of those everyday tasks I truly struggle with. I have a particularly hard time with its evil cousin, ironing. It’s practically impossible for me to get it right, which is why I am very happy that we are entering sweater weather in the northern hemisphere (nobody notices a wrinkled shirt when you’re wearing a not-so-wrinkled sweater over it).
While looking for motivation for the art and science of laundry, I started to realize that it works kinda like algorithms and iteration do, and what’s even better, if we could only get ourselves to think of laundry as we do of those data science concepts, maybe we could get better at them. So, here’s my attempt to bring some Common Sense to a very common data science problem.
Disclosure: before we begin, keep in mind that my goal here is not to give a full technical explanation of algorithms and iteration. If that’s what you’re looking for, this is not the place for you. There are a gazillion articles/books/videos that offer that and much more, so I kindly invite you to stop wasting your time here. If, however, you are interested in unexpected connections between data science and everyday life, read on.
Ok, now that we got the legal stuff out of the way, an algorithm is "a set of instructions used to solve a problem". On the other hand, Iteration is "the process of doing something again and again". OK that’s nice, but the problem is that these are just textbook definitions and (by definition) those are almost never useful in real life. They tend to be just abstract ideas that university courses like to throw at you so they can then charge a bunch of money. So let’s turn to laundry instead.
I want you to imagine that you haven’t done your laundry in a really long time and procrastination has reached its limits. You must face the harsh reality that you can no longer use the same shirt to go out and you MUST take care of your dirtiness, so you throw all your shirts in a huge basket. I also want you to imagine that you have a magical computer that does laundry for you (I’d pay big money for that) and you use it to take care of your dangerously accumulating pile of dirty shirts.
First example
Let’s start by clearly defining what we have here. Let’s say you only own green, blue and yellow shirts:

And you want to wash, dry and iron them, so the basic instructions for your magical laundry computer are:

Pretty straight forward, huh? But the thing is, you’re trying to keep your laundry process organized, so you want to wash, dry and iron the shirts by color. This is conceptually what you would instruct your magical computer to do:

And you’re done. You have successfully washed all your shirts by color. All you did here was execute the instructions on three separate iterations, one for each color.
Second example
Ok now I want you to think that you also have shirts with and without sleeves:

Sticking to what we said in the previous example, these shirts can be green, blue or yellow. Again, being a very organized laundry person, you want to wash your shirts by color, but you also want to do sleeves first and then no-sleeves. These are the instructions for your magical computer:

Now, notice that we have only washed our green shirts (both with and without sleeves), but we haven’t yet touched the other colors. In other words, we maintained the circle "fixed" to the green square, as we iterated on the sleeve variable. So to finish the job, we repeat the procedure for blue shirts:

Again, notice how the circle is "fixed" to the blue square as we iterate on the sleeve variable. Finally, we take care of yellow shirts in exactly the same way:

And that’s it. Basically, you did two different "levels" of iterations in the following way:
- You fixed the circle on the first object of the top iterable (the color green) while you went through the objects of the bottom iterable, executing the instructions on each.
- Then, you just switched to the second element of the top iterable (the color blue), and ran through the objects in the bottom one, executing the instructions again.
- To finish, you repeated the same process, but this time fixing the circle on the third object of the top iterable (the color yellow).
And just like that, you finished your laundry.
Third example
Just because iterations and algorithms are so much fun, let’s assume you also have shirts with and without buttons, which can be of any of the three colors, and can be with or without sleeves (personally, I’ve never seen a sleeveless shirt with buttons but hey, no judgment here).
So, let’s see what the instructions would look like this time:

If you’ve been following along here, you will realize we have only taken care of our green shirts. So we repeat the process for blue ones:

And finally for yellow ones:

The same logic of fixing the circle applies here, only we are now talking about three levels instead of two. Actually, this is kinda the whole point of the article, so let’s expand on it and call it the ‘Laundry Principle’ of iterations and algorithms.
The "Laundry Principle" of iterations and algorithms

So why the heck have you been reading an article about laundry for the last few minutes?
Well, it turns out the above examples help to make iterations and algorithms easier to understand, since they uncover a simple yet very useful idea that we shall from now on call The Laundry Principle.
As we saw at the very beginning, algorithms are just a set of instructions that are passed to a computer to solve a particular problem. But the sequence in which these instructions are executed is what makes algorithms so useful. As we saw above, the instructions are executed in a specific order that allows to tackle problems in a structured an efficient way, and this is where iterative thinking kicks in.
The Laundry Principle: "Bottom-up" beats "Top-down" every day of the week.
The main idea here is that you must always think of multiple-level iterations from the bottom up. Let’s see this in three levels, just like our laundry example. You start with all levels fixed on their first object, except for the last level, which is the one you will shuffle through with your instructions. Let’s call this process A:

Once all objects on the last level are exhausted, you activate iteration on the middle level and repeat process A for each object there. We’ll name this process with the letter B and keep in mind this is a compound process. In other words, process B consists of:
- Executing instructions for all objects in the last level (process A) with a fixed object in the middle level.
- Moving to the next object in the middle level and repeating process A.
- Doing this until all objects in the middle level are exhausted.

Once all objects are exhausted on the middle level, you activate iteration on the top level and repeat compound process B for each object there:

And if you have more levels, well, you keep going on and on for all eternity (or until your iteration levels run out).
It’s important to note that these examples are made very simple with the goal of communicating the idea clearly. As usual, real life is not so gentle, so you will definitely still freak out every now and then when you try to understand someone else’s code. For example, some code snippets will have instructions executed only on specific iteration levels (as opposed to our examples, where instructions were always executed at the bottom), and some iterables might be quite long. However, the principles remain the same.
Laundry wrap-up
What did we learn here? Well, several things:
- Laundry sucks, but you still have to do it eventually (ironing is easier to dodge in the winter).
- Multiple-level iterations are easier to understand when you read them from the bottom-up (The Laundry Principle).
- Real-life code is way more complicated than we would sometimes wish, but having a clear idea about how iteration works will keep you grounded.
So next time you’re stuck trying to understand some dense code on Kaggle, be thankful that at least you’re not doing laundry.