178

I would like to index a list with another list like this

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Idx = [0, 3, 7]
T = L[ Idx ]

and T should end up being a list containing ['a', 'd', 'h'].

Is there a better way than

T = []
for i in Idx:
    T.append(L[i])

print T
# Gives result ['a', 'd', 'h']
Ian Elliott
  • 7,368
  • 5
  • 33
  • 42
Daniel Andrén
  • 1,883
  • 2
  • 13
  • 10
  • 4
    It is really bizarre that `L[idx]` doesn't just work in base Python. Zen of python and all that. In numpy, things like this work just fine. – eric Oct 31 '20 at 15:19
  • @eric A numpy array is vastly different from a CPython list object – pasta_sauce Nov 02 '21 at 15:16

8 Answers8

298
T = [L[i] for i in Idx]
van
  • 68,849
  • 11
  • 156
  • 163
48

If you are using numpy, you can perform extended slicing like that:

>>> import numpy
>>> a=numpy.array(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
>>> Idx = [0, 3, 7]
>>> a[Idx]
array(['a', 'd', 'h'], 
      dtype='|S1')

...and is probably much faster (if performance is enough of a concern to to bother with the numpy import)

Paul
  • 39,902
  • 14
  • 102
  • 121
  • 5
    My quick timeit test showed that using np.array is actually almost 3 times slower (including the conversion to array). – Andrzej Pronobis Aug 10 '16 at 03:57
  • It works better if you need to convert it for array operations anyways. Too time-consuming for regular list operations. – frankliuao Jan 03 '19 at 19:25
  • I tried this approach, i.e. replace `list` with `np.array`, but `np.append` did not work correctly (i.e. the same results as for lists) when the elements themselves were jagged arrays. – James Hirschorn Jan 14 '22 at 16:30
11

A functional approach:

a = [1,"A", 34, -123, "Hello", 12]
b = [0, 2, 5]

from operator import itemgetter

print(list(itemgetter(*b)(a)))
[1, 34, 12]
Padraic Cunningham
  • 168,988
  • 22
  • 228
  • 312
9
T = map(lambda i: L[i], Idx)
mmx
  • 402,675
  • 87
  • 836
  • 780
9

I wasn't happy with any of these approaches, so I came up with a Flexlist class that allows for flexible indexing, either by integer, slice or index-list:

class Flexlist(list):
    def __getitem__(self, keys):
        if isinstance(keys, (int, slice)): return list.__getitem__(self, keys)
        return [self[k] for k in keys]

Which, for your example, you would use as:

L = Flexlist(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
Idx = [0, 3, 7]
T = L[ Idx ]

print(T)  # ['a', 'd', 'h']
jedwards
  • 28,237
  • 3
  • 59
  • 86
  • which also demonstrates the power and flexibility of Python! – crowie Jun 27 '18 at 06:06
  • It's so easy to extend this as well for existing code. Simply call `existing_list = Flexlist(existing_list)` and we have the required functionality without breaking any code – Yesh Jan 09 '20 at 05:53
  • I did `self[int(k)]` instead, so that I could use `np.array`s as indices too. Otherwise, you will get an `np.int64` is not iterable error. – James Hirschorn Jan 14 '22 at 16:58
3

You could also use the __getitem__ method combined with map like the following:

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Idx = [0, 3, 7]
res = list(map(L.__getitem__, Idx))
print(res)
# ['a', 'd', 'h']
David
  • 7,863
  • 2
  • 14
  • 33
1
L= {'a':'a','d':'d', 'h':'h'}
index= ['a','d','h'] 
for keys in index:
    print(L[keys])

I would use a Dict add desired keys to index

Mohammad Yusuf
  • 15,118
  • 9
  • 44
  • 71
user4749532
  • 11
  • 1
  • 3
1

My problem: Find indexes of list.

L = makelist() # Returns a list of different objects
La = np.array(L, dtype = object) # add dtype!
for c in chunks:
    L_ = La[c] # Since La is array, this works.
Hunaphu
  • 385
  • 6
  • 7