Negative subscripts
Greg Ewing
greg.ewing at canterbury.ac.nz
Fri Nov 26 18:37:06 EST 2021
> On 2021-11-26 11:17 AM, Frank Millman wrote:
>> Are there any other techniques anyone can suggest, or is the only
>> alternative to use if...then...else to cater for y = 0?
x[:-y or None]
Seems to work:
>>> l
['a', 'b', 'c', 'd', 'e']
>>> def f(x): return l[:-x or None]
...
>>> f(3)
['a', 'b']
>>> f(2)
['a', 'b', 'c']
>>> f(1)
['a', 'b', 'c', 'd']
>>> f(0)
['a', 'b', 'c', 'd', 'e']
--
Greg
More information about the Python-list
mailing list