TIL: Python Comprehensions Make Matrices Work
Tags: learning, python, todayilearned, adventofcode
Published: 2018-12-04 18:47:05

---
title: TIL: Python Comprehensions Make Matrices Work
published: true
description: Personal struggles with how Python stores data for lists
tags: learning,python,til,adventofcode
---

Hello world!

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](https://adventofcode.com/2018/day/3) 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:

```python
[[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: 

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

which provides you with a list like so: 

```python
[[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— 

```python
shirt[2][1] = 500
```

Which, logically, provides:

```python
[[0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0]]
```

…Sure! Okay! Maybe there’s something funny as a result of multiplying sleeve by 5? 

Let’s make this out of whole cloth, then — 

## The Solution: Take 2

```python
shirt = [[0] * 5] * 5

[[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]]
```

so now it’s all be done in one step. Nothing weird could possibly have happened around the definition of "sleeve", right?

```python
[[0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0]]
```

…okay, sure! Let's try something else -- 

## The Solution: Third Time's The Charm?

```python
sleeve = []
shirt = []

for j in range(5):
    for i in range(1):
        sleeve.append(0)
    shirt.append(arr)

[[ 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]]

shirt[2][1] = 500

[[ 0, 500, 0, 0, 0],
  [0, 500, 0, 0, 0],
  [0, 500, 0, 0, 0],
  [0, 500, 0, 0, 0],
  [0, 500, 0, 0, 0]]
```

![here we are again](https://thepracticaldev.s3.amazonaws.com/i/0o1vizx29791qzl1c05c.gif)

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_.

## The Solution: Sure Why Not!!!

```python
shirt = [[0 for i in range(1000)] for j in range(1000)] 
```
SEVERAL HOURS OF MY LIFE I WILL NEVER GET BACK.

![w h y .](https://thepracticaldev.s3.amazonaws.com/i/1bt1ct8u4tnvoo19rjr3.jpg)