Junior — Middle
How is access to a list element by its index performed?
sobes.tech AI
Answer from AI
Access to a list element in Python is done by index, which indicates the position number of the element, starting from zero. For example, to get the first element of the list my_list, you should use my_list[0].
Example:
my_list = ['apple', 'banana', 'cherry']
print(my_list[1]) # Will output 'banana'
Negative indices allow accessing elements from the end of the list: -1 — the last element, -2 — the second last, and so on.