204

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,588
  • 5
  • 35
  • 42
Daniel Andrén
  • 2,143
  • 2
  • 14
  • 10
  • 9
    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 –  Nov 02 '21 at 15:16
  • @eric on the contrary: it would be really bizarre if `L[idx]` **did** do this in base Python. In fact, I can quote the Zen of Python to support that: "Special cases aren't special enough to break the rules." `L[idx]` **does** "work" - it means that the *tuple* `(0, 3, 7)` will be supplied as an index, which will subsequently cause a `TypeError`. It would work fine with, say, a dict using tuples for its keys. (Slices are different, in that - in prehistoric times - they were a special syntax before there was a `slice` type.) – Karl Knechtel Oct 09 '22 at 06:39
  • “Although practicality beats purity” – eric Oct 10 '22 at 09:49

8 Answers8

330
T = [L[i] for i in Idx]
van
  • 74,297
  • 13
  • 168
  • 171
53

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
  • 42,322
  • 15
  • 106
  • 123
  • 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
14
T = map(lambda i: L[i], Idx)
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
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
  • 176,452
  • 29
  • 245
  • 321
10

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
  • 29,432
  • 3
  • 65
  • 92
  • 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
5

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
  • 8,113
  • 2
  • 17
  • 36
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
  • 16,554
  • 10
  • 50
  • 78
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
  • 589
  • 10
  • 11