TIL: Python Comprehensions Make Matrices Work

First, a super brief introduction: my name is Ry, and I’m midway through a web development bootcamp in Chicago. I was a consultant for the last 5+ years; I wrote a ton of code, but primarily for data analysis/data manipulation in SAS and bending various Microsoft products to my will. Now I’m doing this bootcamp and also boning up on my Python in the hopes of doing some kind of software development/data engineer/????? something other than consulting. I’m still a neophyte (at best) at anything that isn’t SAS or VBA, but I’m working hard and learning a lot and getting better! I'm going to be using this as a blog for things I learned, cool stuff I ran into, projects I'm particularly excited about, etc.

As part of my efforts to git gud, I’ve been doing Advent of Code for the first time! Which is how I ended up finding out something really weird about how Python handles creating lists of lists.

The Problem

Day 3 of Advent of Code involves a bunch of people trying to divide up a sheet of cloth, but they all have different ideas of where to cut and how much to cut. To translate that into less cute and story-relevant terms, I solved this by creating a 2D matrix and finding the coordinates of all overlapping sections within the matrix from the coordinates provided.

Essentially, what I wanted to create was as follows:

[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]

where each 0 represents a single square inch of fabric.

The Solution: Take 1

I cycled through all the coordinates provided in the input; at each square inch covered by a slice, I added +1 to the corresponding 0 in my matrix. By the end, every element in the list with a value of 2 or more was covered by overlapping slices.

This is all quite straightforward; once I had gotten through the spatial reasoning required to understand the question in the first place, writing the code to cycle through everything was relatively trivial. What took me ages, however, was creating the matrix in the first place.

My first thought was to make a blank list. Or:

sleeve=[0]*5shirt=[sleeve]*5

which provides you with a list like so:

[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]

To make sure my coordinates were working correctly (I don’t believe in regex if I don’t really literally have to), I typed in what I naively thought would work to change a single cell—

At this point, I went to the internet and asked what the heck is going on. The answer was, apparently, “use NumPy”, but I am stubborn and determined to figure it out on my own (mistake!).

I — actually still don’t quite know what’s going on. I suspect it has to do with pointers? And somehow the list “sleeve”, when it’s multiplied into the “shirt” list, is just copying in the single sleeve array instead of making copies of sleeve; I haven’t really managed to find any explanation, though, so if you know a ton about python and pointers and how this all works, please (please, for the love of god, this is driving me up the walls) let me know the explanation!

(Is it a pointer? I got the flu during the one (1) CS course I took in college and missed pointer week and I assume all my suffering is a result of that)

Anyway — comprehensions were introduced to me as, “basically a for loop, with some minor differences, which you don’t have to worry about”. Turns out the minor differences you don’t have to worry about are making the damn thing work.

What you're experiencing is a combination of how Python syntax works and the fact that lists are mutable objects.

In the first example:

sleeve=[0]*5shirt=[sleeve]*5

you're telling Python: "make sleeve a list of five numbers and then make shirt a list of five sleeves". So Python is going to copy the object pointed by the variable sleeve inside shirt five time, but it's the same object. You can easily see it like this:

Why does the first [0]*5 not run into the same issues? Is it because this is updating a single list?

That's because Python cheats a little bit. I don't know the exact list of optimizations but you're creating a list of integers so I think it reuses the same object in memory. An integer is an immutable object, it detects you're creating a list of 5 integers, so it just fills up a list of 5 zeroes. You can see what's going on:

The answer is really simple: how many lists do you make? If you really want a 5x5 zero matrix, it's quite obvious you need 6 lists. If you make only 2 of them, some (many) of them are going to be the same.