P
PTY
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
while len(lst) > 0:
lst.pop()
Which is better?
lst = [1,2,3,4,5]
while lst:
lst.pop()
OR
while len(lst) > 0:
lst.pop()
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 lstPTY said:Which is better?
lst = [1,2,3,4,5]
while lst:
lst.pop()
OR
while len(lst) > 0:
lst.pop()
PTY said:Which is better?
lst = [1,2,3,4,5]
while lst:
lst.pop()
OR
while len(lst) > 0:
lst.pop()
while lst:
lst.pop()
Or even just:
lst = []
[]>>> 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
lst = []
;-)
Uh, no, empty lists are False in a boolean context:tac-tics said: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!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.
I'd say the second one. Empty lists are not false.
Steve said:I'll bet you still write
if a>3 == True:
don't you ;-)
lst = [1,2,3,4,5]
while lst:
lst.pop()
Or even just:
lst = []
Peter said:I'll second that.
if (a>3) == True:
is the correct way
Peter
PTY said:Which is better?
lst = [1,2,3,4,5]
while lst:
lst.pop()
OR
while len(lst) > 0:
lst.pop()
No, to be consistent you'll have to write
if ((a>3) == True) == True:
tac-tics wrote: ....
Take them off to where they belong!
Donn said:Tac-tics is right, an empty list is not False.
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.
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
dwelch91 said:
Trueempty_list = []
empty_list is not False
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.