lyst[:None]

Bengt Richter bokr at oz.net
Mon May 26 14:51:51 EDT 2003


On 23 May 2003 15:19:12 +0200, Jacek Generowicz <jacek.generowicz at cern.ch> wrote:

>Jacek Generowicz <jacek.generowicz at cern.ch> writes:
>
>> 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 ...)
>
>lyst[:sys.maxint+1]
<nit>
That seems to work, but you are creating a long when you add one to sys.maxint.
Internally, python seems to use [0:sys.maxint] as a synonym for [:] which is
supposed to get a copy of the whole list. I don't have enough memory to see
if [0]*sys.maxint +[0] can be created, so that [0:sys.maxint+1] would be required
to get all of it. Maybe this is an instance of practicality beating purity ;-)
</nit>

Regards,
Bengt Richter




More information about the Python-list mailing list