list subscripting (nested lists)

Peter Hansen peter at engcorp.com
Sun Mar 31 01:38:31 EST 2002


les ander wrote:
> 
> is it possible to have nested lists indexing in python?
> i.e. index=[3,1,1] and List1=['a','b','c','d']
> then List1[ index ] should print out ['d','b','b']
> 
> I think this would be a cool feature to add if not already present.

It's already present...

>>> index = [3, 1, 1]
>>> list = ['a', 'b', 'c', 'd']

With list comprehensions:

>>> [list[i] for i in index]
['d', 'b', 'b']

Any Python:

>>> map(list.__getitem__, index)
['d', 'b', 'b']

Good enough?



More information about the Python-list mailing list