lyst[:None]

Steve Holden sholden at holdenweb.com
Fri May 23 09:43:46 EDT 2003


"Jacek Generowicz" <jacek.generowicz at cern.ch> wrote in message
news:tyffzn5kdxv.fsf at pcepsft001.cern.ch...
> Given that the name lyst refers to a list, I'd like to limit the
> number of elements returned by the slice
>
>   lyst[:limit]
>
> by setting the value of limit.
>
> Now, what if I want to include the option of getting all the elements
> from the list? Is there a value of limit which would lead to the
> desired behaviour?
>
> -1 does not work: the upper limit is exclusive, so the last element is
> left out
>
> -0 does not work: it just evaluates to 0
>
> None does not work: subscripts must be integers
>
> Is there a way of doing this on one line?
>
> (Yes, I know I can split it up onto several lines, and probably should
> if I want humans to understand the code ...)

As long as you dopn't have too many elements:

>>> lyst = [1, 2, 3, 4]
>>> import sys
>>> lyst[:sys.maxint]
[1, 2, 3, 4]

I'm not even sure whether a list *can* have more than sys.maxint elements -
I'm sure that will give us something to talk about for days!

regards
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/







More information about the Python-list mailing list