2D Lists - Lakelands Computing

Title
Go to content
2D Lists
A 2D, or 2 dimensional list, is effectively a list of lists. Take a look at the tables below and this will make more sense
index
0123
value
cat, dog, pig mice, bird, snake fish, ant ,bear mole, moose, horse
As you can see there is a list of three things in each index place in the list. If we ran this in Python:

  • print (animals[0])

It would print ["cat","dog","pig"]

As it is a list, we can split it down into its indexes too:
index
0123
index012012012012
value
catdogpigmicebirdsnakefishantbearmolemoosehorse
As you can see now there are two index numbers. This allows us to refer to a particular value:

  • print (animals[0][0])

It would print "cat"

  • print (animals[0][1])
would print "dog"

  • print (animals[1][0])
would print "mice"

You might be thinking what's the point of a 2D list, but think of it as maybe a list of contacts the index could be their reference number, and for each customer you could have name, phone, email. Use the trinket below to see how it actually runs. Maybe try and build and use a 2D list for the customer example..

Pay special attention to the way the 2D list is written:

  • There are the [ ] for the main list,
  • Each sublist has its own [ ]
  • The sublists have a , between them so without values it looks like [ [ ] , [ ] ,[ ] ]
  • The values in the sublists are all separated by , too just like they are in a normal (or 1D) list.
All Text copyright Lakelands Academy & Mr T Purslow 2020.
All images copyright free / creative commons unless otherwise stated.
You are welcome to use under a Creative Commons Attribution-nonCommercial-ShareAlike License.
All Text copyright Lakelands Academy & Mr T Purslow 2020.  All images copyright free / creative commons unless otherwise stated. You are welcome to use under a Creative Commons Attribution-nonCommercial-ShareAlike License.
All Text copyright Lakelands Academy & Mr T Purslow 2020.  All images copyright free / creative commons unless otherwise stated. You are welcome to use under a Creative Commons Attribution-nonCommercial-ShareAlike License.
Back to content