List for Beginner

List for Beginner

print (vec [2][1])

Please help,why does
(vec[0]) identifies ele1 ,but not both ele1 AND ele 2
while when we put
(vec[2]) it identifies both subele AND subele 2
Why does sublist is identified as the one whole member and to take separetely subele1,for instance, we should do like that (vec[1])?What is the logic?Please explain,I am a kettle.

You have a three elements list.
The first element of your list is ele1
The second element is ele2
The third element is the list [subele1, subele2], so you have an element that is a list.

Python uses zero base index, that means that start to count from zero, so for the Python interpreter your elements are called in the follow way:

>>>my_list = ['element1', 'element2', ['subelement1', 'subelement2']]
>>> print(my_list[2])
['subelement1', 'subelement2']
>>> print(my_list[2][0])

The last print accesses to the first element of the third element, in zero base index that is the index 2-0

You can google Python Zero base index and you will have lot of information. That applies for all number indexed structures in Python, including strings and lists.


OK,I got this.But why doesn`t python identify ele1 and ele2 the whole list,so 1 whole element in the same way as it identifies subele1 and subele2 one element?What is the difference?Thank you for help!

Elements are separated by commas, if you check after last comma there is a list, it means that the third element is that list - it is a case of nested lists