0

It's possible to slice a python list like this:

>>> list=['a', 'b']
>>> list[0:1]
['a']

However, when passing the index as a string, an error is thrown:

>>> index="0:1"
>>> list[index]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str

How can I specify a list index as a string? What data type is 0:1 in list[0:1], really?

2
  • You should use two integers as the start and stop indexes and then use those. Commented Apr 1, 2016 at 11:02
  • As you can see from the traceback, "list indices must be integers, not str" Commented Apr 1, 2016 at 11:03

4 Answers 4

7

n:m is syntactic sugar for a slice. You could split your index string, convert its parts to integers, and create a slice from those.

>>> lst = list(range(10))
>>> index = "1:4"
>>> s = slice(*map(int, index.split(':')))
>>> lst[s]
[1, 2, 3]

Works just the same with three parts:

>>> index = "1:9:2"
>>> s = slice(*map(int, index.split(':')))
>>> lst[s]
[1, 3, 5, 7]

If you want to allow for "blank" parts, the conversion gets a little bit more involved:

>>> index = "::-1"
>>> s = slice(*[int(x) if x else None for x in index.split(':')])
>>> lst[s]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Sign up to request clarification or add additional context in comments.

3 Comments

@tobias_k...slicing a range will produce a range and not a list in Python3, correct me if I'm wrong?
@IronFist You are right, tried this in Python2; converted to list so it works with both.
1

Why not just convert your slicing string into integers by splitting on :, and then using them as slicing indices.

list=['a', 'b']
slicer_str = '0:1'
slicer_int = [int(i) for i in slicer_str.split(':')]
print(list[slicer_int[0]:slicer_int[1]])

2 Comments

What would happen to a slicer_str of : or :1 in this case?
@Zulakis if such slicer happens to come in your case, you can use map and slice, as suggested by tobias_k. That will handle all such cases.
1

You can use exec() for this.

l = ['a', 'b']
index="0:1"
exec('print(l[{}])'.format(index))

The print is needed only to see the output. You can assign it to a variable and call the variable afterwards instead.

4 Comments

Without the print, i.e. to assign the result to some variable, you should use eval instead of exec. Also, never use eval (or exec) if it can be helped.
exec('a = l[{}]'.format(index)) would assign to a as well
Right, but this way you could not wrap the ugly exec/eval part into a function.
Yes, you are right. But in this particular case, there are no functions involved. In theory you can make a as global in exec() and carry it over even inside a function, it would be super ugly.
0

Use the [ : ] where you specify the start point and end point ,so you can easily trace what you intend to slice

1 Comment

I believe the above answer is not appropriate for the question asked. The comments provided by Mark, etc. are sufficient for problem resolution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.