Coding style

P

PTY

Which is better?

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

while lst:
lst.pop()

OR

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

Simon Brunning

Which is better?

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

while lst:
lst.pop()

OR

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

How about:

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

Or even just:

lst = []

;-)
 
S

Steve Holden

PTY said:
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
 
D

dwelch91

PTY said:
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
 
T

Tim Chase

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

Or even just:

lst = []

Subtly different though...
.... 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
 
S

Steve Holden

tac-tics said:
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
 
D

Dave Hansen

On Mon, 17 Jul 2006 17:09:32 +0100 in comp.lang.python, "Simon

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

Or even just:

lst = []

del lst[:]

is probably closer to what the OP wants...

Regards,
-=Dave
 
R

Roger Miller

Peter said:
I'll second that.

if (a>3) == True:

is the correct way :)

Peter

No, to be consistent you'll have to write

if ((a>3) == True) == True:

Oops, I mean,

if (((a>3) == True) == True) == True:

Umm, never mind.
 
R

rurpy

PTY said:
Which is better?

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

while lst:
lst.pop()

OR

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

A dozen posts, but nobody has posted the right
answer yet, so I will :)

It doesn't matter -- use whichever you prefer (*)
This is an angels on the head of a pin issue.

(*) -- If your code is part of an existing body of
code that uses one or the other style consistently,
then you should do the same.
 
S

Sybren Stuvel

Roger Miller enlightened us with:
No, to be consistent you'll have to write

if ((a>3) == True) == True:

Nah, you want

def is_true(expression):
return expression == True

if is_true((a>3) == True):

Sybren
 
D

Donn Cave

tac-tics wrote: ....
Take them off to where they belong!

Tac-tics is right, an empty list is not False.

Anyway, just for some variety, I think (2) is preferrable
to (1), as is the following

while 1:
try:
lst.pop()
except IndexError:
break

Rather than blindly apply familiar patterns to our work,
I think everyone would agree that coding style in matters
like this should follow the underlying point of the code.
In this case, the body of the test refers implicitly to
the length of the list, since .pop() -> (list[a], list[:a])
where a is (len(list) - 1) It's therefore quite appropriate
for the test to be length.

Donn Cave, (e-mail address removed)
 
E

Erik Max Francis

Donn said:
Tac-tics is right, an empty list is not False.

But that's not what he said. He said it was "not false." That's wrong.
It's false. It's just not False.
 
B

Bob Greschke

PTY said:
Which is better?

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

while lst:
lst.pop()

OR

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

A dozen posts, but nobody has posted the right
answer yet, so I will :)

It doesn't matter -- use whichever you prefer (*)
This is an angels on the head of a pin issue.

(*) -- If your code is part of an existing body of
code that uses one or the other style consistently,
then you should do the same.

I'd go even one step further. Turn it into English (or your favorite
non-computer language):

1. While list, pop.

2. While the length of the list is greater than 0, pop.

Which one makes more sense? Guess which one I like. CPU cycles be damned.
:)

Bob
 
P

PTY

Bob said:
PTY said:
Which is better?

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

while lst:
lst.pop()

OR

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

A dozen posts, but nobody has posted the right
answer yet, so I will :)

It doesn't matter -- use whichever you prefer (*)
This is an angels on the head of a pin issue.

(*) -- If your code is part of an existing body of
code that uses one or the other style consistently,
then you should do the same.

I'd go even one step further. Turn it into English (or your favorite
non-computer language):

1. While list, pop.

2. While the length of the list is greater than 0, pop.

Which one makes more sense? Guess which one I like. CPU cycles be damned.
:)

Bob


It looks like there are two crowds, terse and verbose. I thought terse
is perl style and verbose is python style. BTW, lst = [] was not what
I was interested in :) I was asking whether it was better style to
use len() or not.
 
T

tac-tics

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

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

-Don

Perhaps I should have specified it like this:
empty_list = []
empty_list is not False
True

I'm well aware that both of these snippets does the same thing. I'm
just spouting my opinion that lists and integers are not tests, and in
an ideal world (Java??? X-) if statements support only boolean types.

DISCLAIMER: I do not promote the use of Java.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top