Confused: appending to a list

Discussion in 'Python' started by DataSmash, Mar 23, 2006.

  1. DataSmash

    DataSmash Guest

    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
    DataSmash, Mar 23, 2006
    #1
    1. Advertising

  2. DataSmash wrote:

    > 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
    Diez B. Roggisch, Mar 23, 2006
    #2
    1. Advertising

  3. 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,

    --
    Felipe.
    Felipe Almeida Lessa, Mar 23, 2006
    #3
  4. DataSmash

    Rene Pijlman Guest

    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.

    --
    René Pijlman

    Wat wil jij leren? http://www.leren.nl
    Rene Pijlman, Mar 23, 2006
    #4
  5. DataSmash

    Guest

    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
    , Mar 23, 2006
    #5
  6. DataSmash

    Guest

    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.
    , Mar 23, 2006
    #6
  7. "DataSmash" wrote:

    > 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>
    Fredrik Lundh, Mar 23, 2006
    #7
  8. On 23 Mar 2006 08:31:53 -0800, "DataSmash" <> declaimed
    the following in comp.lang.python:

    >
    > # 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
    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Mar 23, 2006
    #8
  9. DataSmash

    DataSmash Guest

    Thanks for explaining and all the additional ideas!
    R.D.
    DataSmash, Mar 23, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maziar Aflatoun

    Appending to a dropdown list

    Maziar Aflatoun, Sep 22, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    728
    Maziar Aflatoun
    Sep 23, 2004
  2. Gernot Frisch

    appending list at startup?

    Gernot Frisch, Jan 12, 2005, in forum: C++
    Replies:
    2
    Views:
    309
    Gernot Frisch
    Jan 12, 2005
  3. sud jag

    appending to list

    sud jag, Oct 1, 2003, in forum: Python
    Replies:
    4
    Views:
    510
    Michael Hudson
    Oct 1, 2003
  4. Debajit Adhikary
    Replies:
    17
    Views:
    674
    Debajit Adhikary
    Oct 18, 2007
  5. Prasad, Ramit
    Replies:
    0
    Views:
    231
    Prasad, Ramit
    Oct 25, 2012
Loading...

Share This Page