Fetching multiple items from a list

Emile van Sebille emile at fenx.com
Tue Jul 3 08:35:07 EDT 2001


Probably because it's so easy to create that functionality:

class MyList:
    def __init__(self,l):
        self.lst = l
    def __call__(self,*args):
        return [self.lst[i] for i in args]

mylist = MyList(list(string.lowercase))
print mylist(18,15,0,12)

--

Emile van Sebille
emile at fenx.com

---------
"Joonas Paalasmaa" <joonas.paalasmaa at nokia.com> wrote in message
news:3B41B778.6BD422C1 at nokia.com...
> Why doesn't Python support fetching and setting multiple items
> at the same time for lists and tuples.
>
> For example in the example below multiple fetching would be
> much better way than the ordinary way.
>
> >>> import string
> >>> mylist = list(string.lowercase)
> >>> mylist[18],mylist[15],mylist[0],mylist[12]
> ('s', 'p', 'a', 'm')
> >>> mylist[18,15,0,12]
> Traceback (most recent call last):
>   File "<pyshell#23>", line 1, in ?
>     mylist[18,15,0,12]
> TypeError: sequence index must be integer
> >>>





More information about the Python-list mailing list