Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Coding style

Reply
Thread Tools

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()

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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()


How about:

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/
 
Reply With Quote
 
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

 
Reply With Quote
 
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
 
Reply With Quote
 
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




 
Reply With Quote
 
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.

 
Reply With Quote
 
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
 
Reply With Quote
 
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

 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
general coding issues - coding style... calmar Python 11 02-21-2006 10:36 AM
Coding style for CPLD vs FPGA Analog Guy VHDL 3 03-14-2005 08:26 AM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Coding style to prioritize certain inputs Willem Oosthuizen VHDL 5 09-04-2003 05:17 PM
style for coding latches Paul Baxter VHDL 7 08-15-2003 06:19 PM



Advertisments