Beginners question.

Discussion in 'Python' started by Player, Aug 31, 2004.

  1. Player

    Player Guest

    Hi all

    I am teaching myself python and so am following a few tutorials from
    online.

    In one of the tutorials an exercise is the following...

    As an exercise, write a loop that traverses the previous list and prints
    the length of each element.

    Firstly I just wanted to see if I could get the length out of those elements
    that were list type.
    I no thats not exactly what the exercise asks for, but I thought I would
    start there.
    However I am stuck and cant figure out why my code is doing what it's doing.
    My code is as follows....

    mylist = [1, 2, ["three", "four"], "five"]
    for item in mylist:
    if item == type(list):
    myitem = len(item)
    print myitem
    else:
    print item

    All my code seems to do is print out each element of the list on a new line.
    What I had expected it to do was this....
    1
    2
    2
    'five'

    but it doesn't, what it does do is this...

    1
    2
    ['three', 'four']
    'five'

    I expected the line,
    myitem = len(item)
    to issue the variable, myitem, with a value that was the length of item.
    But as you cans ee it doesn't and simply ignores all that code and does the
    else:
    print item
    part instead, misisng out the first half of the for loop.

    Were am I going wrong ??

    Thanks in advance for any help :)

    M.B aka - Player -

    --
    *************
    The Imagination may be compared to Adam's dream-
    he awoke and found it truth.
    John Keats.
    *************
    Player, Aug 31, 2004
    #1
    1. Advertising

  2. Player

    Russell Blau Guest

    "Player" <> wrote in message
    news:ch2c6b$5ap$...
    > Firstly I just wanted to see if I could get the length out of those

    elements > that were list type.
    > I no thats not exactly what the exercise asks for, but I thought I would
    > start there.
    > However I am stuck and cant figure out why my code is doing what it's

    doing.
    > My code is as follows....
    >
    > mylist = [1, 2, ["three", "four"], "five"]
    > for item in mylist:
    > if item == type(list):


    The "if" statement above is always false (well, at least for any list you
    are likely to encounter in real life), because you are looking to see if the
    item is equal to "type(list)" which is a type object. I think you meant:

    if type(item) == list:

    That's why your len() statement was never being reached.

    > myitem = len(item)
    > print myitem
    > else:
    > print item
    >


    --
    I don't actually read my hotmail account, but you can replace hotmail with
    excite if you really want to reach me.
    Russell Blau, Aug 31, 2004
    #2
    1. Advertising

  3. On Tue, 31 Aug 2004 18:23:23 +0100, Player wrote:

    > mylist = [1, 2, ["three", "four"], "five"]
    > for item in mylist:
    > if item == type(list):
    > myitem = len(item)
    > print myitem
    > else:
    > print item


    General tip: Don't forget how much fun the interpreter is:

    Python 2.3.4 (#1, Jun 8 2004, 17:41:43)
    [GCC 3.3.3 20040217 (Gentoo Linux 3.3.3, propolice-3.3-7)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> item = ["three", "four"] # since this is the one we wonder about
    >>> item == type(list)

    False
    >>> # Uh-oh, there's the problem, let's see if we can get a clue:

    ....
    >>> item

    ['three', 'four']
    >>> type(list)

    <type 'type'>
    >>> # Clearly, there's something wrong here; try Russell's solution:

    ....
    >>> type(item)

    <type 'list'>
    >>> list

    <type 'list'>
    >>> # Ah ha!

    ....
    >>>
    Jeremy Bowers, Aug 31, 2004
    #3
  4. Player

    Player Guest

    [edit:snip]

    Ok thanks Russell Blau :) I can see it now heh.

    M.B -aka- Player
    Player, Aug 31, 2004
    #4
  5. Player <> wrote:
    ...
    > if item == type(list):


    It's very unlikely that the item will equal the type of list (which is
    the builtin metaclass 'type' itself). You presumably mean:

    if isinstance(item, list):


    Alex
    Alex Martelli, Sep 1, 2004
    #5
    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. Alex
    Replies:
    1
    Views:
    268
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Apr 19, 2004
  2. Mike P

    web service beginners question

    Mike P, Aug 18, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    735
    S. Justin Gengo
    Sep 21, 2004
  3. Roger stevenson

    Beginners Question Regarding Static Web Pages

    Roger stevenson, Apr 30, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    392
    Sam Samnah
    Jul 1, 2005
  4. Child
    Replies:
    2
    Views:
    1,013
    souri challa
    Jun 1, 2005
  5. =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=

    Late night beginners question

    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Jul 31, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    346
    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=
    Jul 31, 2005
Loading...

Share This Page