Explicitly select items from a Python list or tuple

I have the following Python list (there could also be a tuple):

myList = ['foo', 'bar', 'baz', 'quux'] 

I can tell

 >>> myList[0:3] ['foo', 'bar', 'baz'] >>> myList[::2] ['foo', 'baz'] >>> myList[1::2] ['bar', 'quux'] 

How do I explicitly highlight elements whose indices do not have specific patterns? For example, I want to select [0,2,3] . Or from a very large list of 1000 elements, I want to select [87, 342, 217, 998, 500] . Is there any Python syntax? Something similar:

 >>> myBigList[87, 342, 217, 998, 500] 
+66
python list select indexing tuples
Jul 09 2018-11-11T00:
source share
7 answers
 list( myBigList[i] for i in [87, 342, 217, 998, 500] ) 



I compared the answers with python 2.5.2:

  • 19.7 usec: [ myBigList[i] for i in [87, 342, 217, 998, 500] ]

  • 20.6 usec: map(myBigList.__getitem__, (87, 342, 217, 998, 500))

  • 22.7 usec: itemgetter(87, 342, 217, 998, 500)(myBigList)

  • 24.6 usec: list( myBigList[i] for i in [87, 342, 217, 998, 500] )

Note that in Python 3, 1st was changed the same as on 4th.




Another option is to start with numpy.array , which allows indexing through a list or numpy.array :

 >>> import numpy >>> myBigList = numpy.array(range(1000)) >>> myBigList[(87, 342, 217, 998, 500)] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: invalid index >>> myBigList[[87, 342, 217, 998, 500]] array([ 87, 342, 217, 998, 500]) >>> myBigList[numpy.array([87, 342, 217, 998, 500])] array([ 87, 342, 217, 998, 500]) 

tuple does not work like the ones that are slicers.

+85
Jul 09 2018-11-11T00:
source share
β€” -

How about this:

 from operator import itemgetter itemgetter(0,2,3)(myList) ('foo', 'baz', 'quux') 
+29
Jul 09 2018-11-11T00:
source share

It is not built-in, but you can subclass a list that accepts tuples as "indexes" if you want:

 class MyList(list): def __getitem__(self, index): if isinstance(index, tuple): return [self[i] for i in index] return super(MyList, self).__getitem__(index) seq = MyList("foo bar baaz quux mumble".split()) print seq[0] print seq[2,4] print seq[1::2] 

Print

 foo ['baaz', 'mumble'] ['bar', 'quux'] 
+9
Jul 09 2018-11-11T00:
source share
 >>> map(myList.__getitem__, (2,2,1,3)) ('baz', 'baz', 'bar', 'quux') 

You can also create your own List class that supports tuples as arguments to __getitem__ if you want to be able to do myList[(2,2,1,3)] .

+4
Jul 09 2018-11-11T00:
source share

Perhaps a list comprehension is fine:

 L = ['a', 'b', 'c', 'd', 'e', 'f'] print [ L[index] for index in [1,3,5] ] 

It produces:

 ['b', 'd', 'f'] 

Is this what you are looking for?

+3
Jul 09 '11 at 2:00
source share

I just want to point out, even the itemgetter syntax looks very neat, but it is slow when executed in a large list.

 import timeit from operator import itemgetter start=timeit.default_timer() for i in range(1000000): itemgetter(0,2,3)(myList) print ("Itemgetter took ", (timeit.default_timer()-start)) 

Considered that he received 1.065209062149279

 start=timeit.default_timer() for i in range(1000000): myList[0],myList[2],myList[3] print ("Multiple slice took ", (timeit.default_timer()-start)) 

Several fragments took 0.6225321444745759

0
Nov 01 '16 at 14:50
source share

Another possible solution:

 sek=[] L=[1,2,3,4,5,6,7,8,9,0] for i in [2, 4, 7, 0, 3]: a=[L[i]] sek=sek+a print (sek) 
0
Nov 18 '17 at 20:32
source share



All Articles