0
Can i set a list to start indexing from 1, what i mean is that, instead the first element to be [0], it would be [1]
list index
4 odpowiedzi
+ 8
class MyList(list):
def __getitem__(self, n):
return super().__getitem__(n-1)
a = MyList(range(10))
print(a)
print(a[4])
+ 7
No .Python list start from 0th index.
+ 5
I guess you can build your own class on top of the regular list and just overwrite the magic methods related to indexing. Take a look in here:
https://rszalski.github.io/magicmethods/#sequence
But then again -- why would you? :)
+ 2
You can use enumerate with a custom offset. Here, the first element is element 1:
https://code.sololearn.com/cbH3QRT5RIzs/?ref=app
You can also use this to make a list of 1-based tuples:
print(list(enumerate(l, 1)))