Confused: appending to a list

D

DataSmash

I'm confused. Why is it that when I say "while len(list) < 5:", I get
5 items in my list.
If I say "while len(list) < 6:", I get 6 items in the list and so on.
I would think if I said "less than 5", I would get 4 items.
Can anyone explain this?
Thanks.
R.D.


# Start an empty list
list = []
while len(list) < 5:
# Get a random number between 1 & 100
num = random.randint(1,100)
# Make sure there are no duplicates
if num not in list:
# Append each number to the list
list.append(num)
print list
 
D

Diez B. Roggisch

DataSmash said:
I'm confused. Why is it that when I say "while len(list) < 5:", I get
5 items in my list.
If I say "while len(list) < 6:", I get 6 items in the list and so on.
I would think if I said "less than 5", I would get 4 items.
Can anyone explain this?

Yes - you loop until the condition is _not_ fullfilled anymore. Which means
that if the list has a length of five, the len(l) < 6 is true, and
appending makes it len(l) == 6 - which then will fail your condition.

So - you need to loop one time less, by doing

while len(l) < 6 - 1:
...

diez
 
F

Felipe Almeida Lessa

Em Qui, 2006-03-23 às 08:31 -0800, DataSmash escreveu:
I'm confused. Why is it that when I say "while len(list) < 5:", I get
5 items in my list.

"while len(list) < 5:" implies that the loop will stop only when
len(list) >= 5.

HTH,
 
R

Rene Pijlman

DataSmash:
I'm confused. Why is it that when I say "while len(list) < 5:", I get
5 items in my list.

Because the last time when len(list) was < 5, the block of code following
the while executed and did something to the list to give it a length >= 5
(otherwise the block of code would be executed again and it wouldn't have
been the last time).
list.append(num)

There you have it.
 
S

skip

R.D.> I'm confused. Why is it that when I say "while len(list) < 5:", I
R.D.> get 5 items in my list. If I say "while len(list) < 6:", I get 6
R.D.> items in the list and so on. I would think if I said "less than
R.D.> 5", I would get 4 items. Can anyone explain this? Thanks. R.D.

The loop exits when the condition becomes false. That happens when
len(list) >= 5 (or 6).

Skip
 
A

adam.bachman

You're wanting it to stop when the len(list) == 4, right? The easiest
way to change the logic would be to say

while len(list) != 4:

but that could get you into trouble later on. The problem with the
len(list) < 5 expression is that the loop will run "one more time" as
long as len(list) == 4 adding another item to the list giving you one
more than you wanted. If you wanted, you could put your len() check
inside the loop:

# Start an empty list
list = []
while 1:
# Get a random number between 1 & 100
num = random.randint(1,100)
# Make sure there are no duplicates
if num not in list:
# Append each number to the list
list.append(num)
if len(list) == 4:
# Break if we've reached desired length.
break
print list

I hope this gives you some ideas.
 
F

Fredrik Lundh

DataSmash said:
I'm confused. Why is it that when I say "while len(list) < 5:", I get
5 items in my list.
If I say "while len(list) < 6:", I get 6 items in the list and so on.
I would think if I said "less than 5", I would get 4 items.

except that you're saying "as long as there are less than 5 items
in the list, add another one"

</F>
 
D

Dennis Lee Bieber

# Start an empty list
list = []
while len(list) < 5:

The empty list has a length of 0...

0
1
2
3
4 4 < 5, but is the fifth element of the list
# Get a random number between 1 & 100
num = random.randint(1,100)
# Make sure there are no duplicates

You want no duplicates? Try this (untested):

lst = [] #don't use "list", that's a data type
for x in xrange(4): #use however many elements you really want
while True:
num = random.randint(1, 100)
if num not in lst:
lst.append(num)
break #added element, go back to outer loop
--
 

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,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top