Getting a specific thing out of a list

I am very, very confused, when I do
self.breach_strings.append(state["events"]["breach"][0])

It outputs [[[1, 12], 1.0, 3, '39', 2]]

But if I do self.breach_strings.append(state["events"]["breach"][0][0])
It outputs [1, 12]

Why does it skip like that, and how do I get the one in the middle?

This is just how nested indexing works. Your first call returned an entire list. To make things more clear, let’s give it a name.

first_breach = state["events"]["breach"][0]

Now first_breach is that list that got printed: [ [1,12], 1.0, 3, β€˜39’, 2 ]
This list has 5 things in it, at indexes 0, 1, 2, 3, and 4.

So first_breach[0] will be equal to [1,12].

You may want to read some Python websites about how each data structure works:

All of chapter 5 here:
https://docs.python.org/3.6/tutorial/datastructures.html#

And also just google around for third-party info articles like this:
http://thomas-cokelaer.info/tutorials/python/data_structures.html

1 Like