Coding style

Bruno Desthuilliers onurb at xiludom.gro
Tue Jul 18 04:58:48 EDT 2006


dwelch91 wrote:
> PTY wrote:
> 
>> Which is better?
>>
>> lst = [1,2,3,4,5]
>>
>> while lst:
>>   lst.pop()
>>
>> OR
>>
>> while len(lst) > 0:
>>   lst.pop()
>>
> 
> I think the first one is better, but if all you are doing is removing
> all the items in the list, this is definitely better:
> 
> lst = []

Not if there are other names bound to the same list. You are only
rebinding this name, which does *not* empty the list object. The correct
solution here is

del lst[:]

which will remove all content from the list object,



-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list