Velocity Reviews > Coding style

# Coding style

PTY
Guest
Posts: n/a

 07-17-2006
Which is better?

lst = [1,2,3,4,5]

while lst:
lst.pop()

OR

while len(lst) > 0:
lst.pop()

Sybren Stuvel
Guest
Posts: n/a

 07-17-2006
PTY enlightened us with:
> Which is better?
>
> lst = [1,2,3,4,5]
>
> while lst:
> lst.pop()
>
> OR
>
> while len(lst) > 0:
> lst.pop()

The first one.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa

Simon Brunning
Guest
Posts: n/a

 07-17-2006
On 17 Jul 2006 08:56:34 -0700, PTY <(E-Mail Removed)> wrote:
> Which is better?
>
> lst = [1,2,3,4,5]
>
> while lst:
> lst.pop()
>
> OR
>
> while len(lst) > 0:
> lst.pop()

lst = [1,2,3,4,5]
while lst:
lst.pop()

Or even just:

lst = []

--
Cheers,
Simon B,
http://www.velocityreviews.com/forums/(E-Mail Removed),
http://www.brunningonline.net/simon/blog/

Steve Holden
Guest
Posts: n/a

 07-17-2006
PTY wrote:
> Which is better?
>
> lst = [1,2,3,4,5]
>
> while lst:
> lst.pop()
>
> OR
>
> while len(lst) > 0:
> lst.pop()
>

The former, without a doubt. It says exactly the same thing, since lst
can only be considered false when it is empty. Experienced Python
programmers would scratch their heads at your second formulation.

I doubt there's much in it from a time point of view (though I know as I
write this it will spur someone to use timeit.py to point out I am wrong).

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

dwelch91
Guest
Posts: n/a

 07-17-2006
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 = []

-Don

Tim Chase
Guest
Posts: n/a

 07-17-2006
> lst = [1,2,3,4,5]
> while lst:
> lst.pop()
>
> Or even just:
>
> lst = []

Subtly different though...

>>> while lst:

.... lst.pop()
....
5
4
3
2
1
>>> lst2

[]
>>> lst = [1,2,3,4,5]
>>> lst2 = lst
>>> lst = []
>>> lst2

[1, 2, 3, 4, 5]
>>> lst = [1,2,3,4,5]
>>> lst2 = lst
>>> del lst[:]
>>> lst2

[]

The original while loop changes the actual list, reassigning it
to a new list prevents other items that reference that list from
accessing the changes. As shown above, I recommend

del lst[:]

which should be as fast as python will let one do it. (maybe?
again with those timeit guys...

-tkc

tac-tics
Guest
Posts: n/a

 07-17-2006
> Or even just:
>
> lst = []
>
>

Indeed.

I'd say the second one. Empty lists are not false. They are empty. Long
live dedicated boolean data types.

dwelch91
Guest
Posts: n/a

 07-17-2006
tac-tics wrote:
>
> I'd say the second one. Empty lists are not false. They are empty. Long
> live dedicated boolean data types.
>

Uh, no, empty lists are False in a boolean context:

http://docs.python.org/lib/truth.html

-Don

Steve Holden
Guest
Posts: n/a

 07-17-2006
tac-tics wrote:
>>Or even just:
>>
>>lst = []
>>
>>

>
>
> Indeed.
>
> I'd say the second one. Empty lists are not false. They are empty. Long
> live dedicated boolean data types.
>

Take them off to where they belong!

I'll bet you still write

if a>3 == True:

don't you

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sybren Stuvel
Guest
Posts: n/a

 07-17-2006
tac-tics enlightened us with:
> I'd say the second one. Empty lists are not false.

Yes they are. See the discussion last week about this topic.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa