I have a question about accessing elements in lists.
This is the code:
movies = ["The Holy Grail", 1975, "Terry Jones and Terry Gilliam", 91,["Graham Champman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]]print(movies[4][1][3])
And this is the output: Eric Idle
My question is why is the output Eric Idle
? What does 4
represent, what do 1
and 3
represent? I'm so confused.
Your list is separated into values.
# movies: values
0. "The Holy Grail"
1. 1975
2. "Terry Jones and Terry Gilliam"
3. 91
4. ["Graham Champman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]
/!\ The index begin from 0
The last value is also separated into values:
# movies[4]: values
0. "Graham Champman"
1. ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]
And the last value is also separated into other values:
# movies[4][1]: values
0. "Michael Palin",
1. "John Cleese"
2. "Terry Gilliam"
3. "Eric Idle"
4. "Terry Jones"
So calling movies[4]
returns the last element of movies
:
["Graham Champman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]
Typing movies[4][1]
returns this:
["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]
And typing movies[4][1][3]
returns that:
"Eric Idle"
Tree view
movies0. | "The Holy Grail"1. | 19752. | "Terry Jones and Terry Gilliam"3. | 914. |____4.0. | "Graham Champman"4.1. |____4.1.0 | "Michael Palin"4.1.1 | "John Cleese"4.1.2 | "Terry Gilliam"4.1.3 | "Eric Idle"4.1.4 | "Terry Jones"
Hope that helped.