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

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to