Creating slice notation from string

Bob van der Poel bob at
Thu Sep 3 00:55:10 CEST 2009

> For a one-liner:
>    x[slice(*map(int, x[1:-1].split(':')))]


Almost works :)

For s="[2]" and s="[1:2]" it's fine. But, if I have

s = "[:2]" then I get:

>>> x[slice(*[int(i) for i in s.strip("[]").split(":")])]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

Similar problem with  [2:].


More information about the Python-list mailing list