List syntax question

Hi Everyone,

This isn’t related to a course on Codecademy, but I saw some syntax the other day and I’m not sure how it works.

list = [1, ,2, 3, 4, 5, 6, 7]

print(list[list[4]])

This prints 6. Can someone explain to me what is happening here pleasae?

thank you

lets name these outer and inner index lookup. To resolve the outer list lookup, python first need to resolve the inner index lookup (lst[4]). This gives the value at index 4, which is 5.

then we use this value as index to again lookup a value in the list, this time at index 5. Which gives 6.

you could also do this in steps to make it easier for yourself:

step = list[4]
print(step)
print(list[step])
2 Likes

Ohhhh it’s so simple, thank you very much