28

Is there any way in python to use a tuple as the indices for a slice? The following is not valid:

>>> a = range(20)
>>> b = (5, 12)   # my slice indices
>>> a[b]          # not valid
>>> a[slice(b)]   # not valid
>>> a[b[0]:b[1]] # is an awkward syntax
[5, 6, 7, 8, 9, 10, 11]
>>> b1, b2 = b
>>> a[b1:b2]      # looks a bit cleaner
[5, 6, 7, 8, 9, 10, 11]

It seems like a reasonably pythonic syntax so I am surprised that I can't do it.

jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
Mike
  • 2,497
  • 3
  • 26
  • 40

4 Answers4

44

You can use Python's *args syntax for this:

>>> a = range(20)
>>> b = (5, 12)
>>> a[slice(*b)]
[5, 6, 7, 8, 9, 10, 11]

Basically, you're telling Python to unpack the tuple b into individual elements and pass each of those elements to the slice() function as individual arguments.

Chinmay Kanchi
  • 58,811
  • 22
  • 84
  • 113
13

How about a[slice(*b)]?

Is that sufficiently pythonic?

Eugene Yarmash
  • 131,677
  • 37
  • 301
  • 358
Foon
  • 5,830
  • 11
  • 38
  • 41
  • I had completely forgotten about python's `*args` syntax. It would be more pythonic though if I could just write `a[*b]`. Of course this would conflict with multi-dimensional arrays so the ordinary slicing syntax is still better. Inclusion of the word slice is longer but probably far more clearly illustrates the purpose of the code. – Mike Aug 15 '11 at 22:19
4

slice takes up to three arguments, but you are only giving it one with a tuple. What you need to do is have python unpack it, like so:

a[slice(*b)]
Ethan Furman
  • 57,917
  • 18
  • 142
  • 218
2

Only one tiny character is missing ;)

In [2]: a = range(20)

In [3]: b = (5, 12)

In [4]: a[slice(*b)]
Out[4]: [5, 6, 7, 8, 9, 10, 11
Martin Thurau
  • 7,428
  • 7
  • 40
  • 77