Confused: appending to a list

DataSmash rdh at new.rr.com
Thu Mar 23 17:31:53 CET 2006


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




More information about the Python-list mailing list