+ 3
Python - is list's index() method 1-based instead of 0-based?
Consider the following output: a = list() a.append(7) a.extend([4,2]) a += [5] a.insert(0,3) print(a) print(a.index(4))
2 odpowiedzi
+ 5
The resulting list is
[3,7,4,2,5]
a.index(4) returns 2.
Quoting the docs, https://docs.python.org/3.7/tutorial/datastructures.html
list.index(x) "returns zero-based index in the list of the first item whose value is equal to x. "
This agrees with the observed output. What a.index(4) does, is that it searches the list for the first element of value 4 occuring in the list, and returns the index of that element.
+ 4
I was confused by "2" in list 😅