Understanding `list[x][y]`

Issue

I was looking at making a maze game in python, and on an online tutorial there was a list that seemed to be indexed twice. I understand that list[x] pulls the x item from that list, but I don’t know how double brackets like list[x][y] work.
Does it pull the y of the x of list?

Can someone please tell me what it’s called so I can further research it?

Solution

Yes, you are correct. Imagine we have a list with sublists in them. For example:

a = [[1,2,3],[4,5,6],[7,8,9]]

If we want to access one of those numbers, let’s say 6, and assign it to the variable b, we would select it like this:

b = a[1][2]

The one means the 2nd element in the list a and the two would mean the 3rd element in the list we selected from a.

I hope this solves your problem and happy coding!

Answered By – Afzain123

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published