lyst[:None]

Alex Martelli aleaxit at yahoo.com
Fri May 23 09:35:04 EDT 2003


Jacek Generowicz wrote:

> 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?

sys.maxint works.  You do have to have imported sys previously, normally,
but if you're keen to avoid that, and don't care for readability,
__import__('sys').maxint should be equivalent.


Alex





More information about the Python-list mailing list