Accessing Elements in Nested Lists
Accessing Elements in Nested Lists
my_list = ['a', 'b', 'c', 1, 2, 3, ['apple', 'orange', 'bananana'], 'd']
If you wanted to print banana, how would you do this?
# Long way:
extracted_list = my_list[6]
print(extracted_list[2])
# Short way:
extracted_fruit = my_list[6][2]
List elements can be changed for nested lists as well:
my_list[6][2] = 'kiwi'
Last updated