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

14th Sep 2018, 4:36 PM
Joseph Ojo
Joseph Ojo - avatar
4 Answers
+ 8
class MyList(list): def __getitem__(self, n): return super().__getitem__(n-1) a = MyList(range(10)) print(a) print(a[4])
14th Sep 2018, 7:59 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 7
No .Python list start from 0th index.
14th Sep 2018, 4:43 PM
Mitali
Mitali - avatar
+ 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? :)
14th Sep 2018, 4:56 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 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)))
14th Sep 2018, 7:44 PM
Anna
Anna - avatar