Dark theme

Containers


So, how might we get rid of y0 and x0?

One way is like this:

agents = []
agents.append([random.randint(0,99),random.randint(0,99)])

This way, we never need to create y0 and x0. This is cleaner and more efficient. The only downside is that we lose any indication of what the data means. For the moment, deal with this by putting in some comments. We'll come back to embedding that information in variable names later.


Once you've commented the code, go through and do the same thing for y1 and x1, using the same agents list (i.e. *don't* reset it to an empty list using agents = []). We want to append a new set of coordinates into the same list; each set of coordinates will represent an agent. We also need to go through and replace y1 and x1 with indexed list references as before, noting that we're now dealing with the second set of coordinates in our agents list. Check your new code works still.

We should end up with a list something like this:

[[34,22],[27,51]]

Note carefully the location of the commas. This is a 2D list, each position in the first dimension being a sublist, as we can see if we write it like this:

[
[34,22],
[27,51]
]


  1. Introduction
  2. Making lists I
  3. This page
  4. Using functions <-- Next