+ 1

Python3 Module 3 Quiz, Q1

"What is the output of this code? list = [1, 1, 2, 3, 5, 8, 13] print(list[list[4]])" Why is the answer 8? The first list[4] tells it to index the item in [4] (which is 5 in this case), but what does the outer list[] in list[list[4]] do? Thanks.

9th Jan 2021, 9:49 PM
V
V - avatar
2 Answers
+ 4
list[list[4]] == list[5] list[5] == 8 The first slice (list[4]) selects 5 from the list. Remember slice count starts from 0. list[5] selects 8
9th Jan 2021, 10:06 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
The outer list is asking us to index to the answer of the inner [list[4]] You are correct the inner list indexed to 4, is 5. Now the outer list can be simplified to be read as (list[5])
5th Jan 2022, 8:50 PM
michael macias