I am asking this question related to this one:
Access multiple elements of list knowing their index
Basically if you have a list and want to get several elements you cannot just pass a list of indexes.
Example:
a = [-2,1,5,3,8,5,6]
b = [1,2,5] # list of indexes
a[b] #this doesn't work
# expected output: [1,5,5]
To solve this problem several options are proposed in the linked question:
Using list comprehension:
a = [-2,1,5,3,8,5,6]
b = [1,2,5]
c = [a[i] for i in b]
Using operator.itemgetter
:
from operator import itemgetter
a = [-2, 1, 5, 3, 8, 5, 6]
b = [1, 2, 5]
print itemgetter(*b)(a)
or using numpy arrays (which DO accept indexing with lists)
import numpy as np
a = np.array([-2, 1, 5, 3, 8, 5, 6])
b = [1, 2, 5]
print list(a[b])
My question is: Why normal lists don't accept this? This will not conflict the normal indexing [start:end:step]
and will provide another way of accessing list elements without using external libraries.
I don't intend this question to attract opinion based answers but rather to know if there is an specific reason why this feature is not available in Python, or if it will be implemented in a future.