On 11/24/20 2:36 PM, Mathew M. Noel via Python-ideas wrote: > > Python uses an index of -1 to index the last element in a list. Since > -1 occurs before 0 we might think of the elements of the linear list > are being bent into a circle making the last element occur before the > 0th element. Consider a list with n elements: it would be perfectly > reasonable to address the element 0 of the list using an index of n > since n occurs after n-1 (if we assume that the list is bent into a > circle). This feature can prove to be extremely useful. Consider the > following example: > > > days_of_the_week = > ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"] > It would be nice if > days_of_the_week[0] > is the same as > days_of_the_week[7] is the same as > days_of_the_week[14] etc In other words use modular indexing. In other > words if the index is outside the range 0 to n-1, we simply take the > remainder when the index is divided by n as the index. Because of the > close relationship between finite length sequences and periodic > sequences this feature might simplify scientific computing(circular > convolution etc).
If I wanted this sort of indexing, using % works very well. Losing the error detection of out of bounds indexing would be very bad in most cases. As I think about this, the only cases that I would likely do this will have a fixed sized list (like the 7 days of the week) it makes it fairly trivial. -- Richard Damon _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/4JY7TYHM3FZYEFSPVTOGBXZQETAWZPSZ/ Code of Conduct: http://python.org/psf/codeofconduct/