looping list problem

Discussion in 'Python' started by Jon Bowlas, Aug 16, 2005.

  1. Jon Bowlas

    Jon Bowlas Guest

    HI all,

    I'm fairly new to python and programming in general so I was hoping someone
    here may be able to help me. Let me explain what the problem I'm having is:

    I am trying to parse the XML of an attributes object I created, this object
    has the structure outlined below. Everything is ok on the parsing front
    until I try to get the values in ucl_navhide (StringField); these are
    basically the id's of objects I wish to hide in a website navigation menu
    separated by a space:

    <ucl_title>root atts</ucl_title>
    <ucl_website_name>UCL Web Services</ucl_website_name>

    <ucl_navhide>test1 test2</ucl_navhide>

    I have a script 'normalmenu' that I will eventually be using to generate a
    navigation menu for a website here it is in its present development state:

    attobject = context.get_attobject()
    navstring = context.get_uclattribute(attobject, 'ucl_navhide')
    hiddennavelements = navstring.split(' ')
    for hiddennavelement in hiddennavelements:
    return hiddennavelement

    So the script 'get_attobject' basically looks for an instance of the
    attributes object in the current folder, if it doesn't locate one then it
    uses acquisition to find one in a parent folder. The script
    'get_uclattribute' then gets the nodeValues of the requested node. In this
    instance its ucl_navhide, then I split the 'navstring' string at the spaces
    and attempt the for-loop to output each of the values.

    Unfortunately it appears I am unable to loop through each of the list items
    in hiddennavelements, as it only returns the first value & will not repeat.

    Strangely if I test to output the value of hiddennavelements it looks like
    this: [u'test1', u'test2'] which I believe the u refers to Unicode, although
    I could be wrong. Even more bizarrely if I test the len(hiddennavelements)
    it returns the correct result (2), so why wont my for-loop work?

    Hope someone can help, or point out my schoolboy error.

    Jon Bowlas, Aug 16, 2005
    1. Advertisements

  2. Paul McGuire

    Paul McGuire Guest

    Well, you are returning prematurely from a for loop, so that is why you
    are only getting the first value. Its just like:

    for i in range(1000000):
    return i

    It doesn't matter how big the range is you are iterating over, you'll
    return on the first element and that's it.

    If what you want is the list, then return the list:

    hiddennavelements = navstring.split(' ')
    return hiddennavelements

    I think Fredrik Lundh was trying to accommodate your mixed thinking by
    assuming your code was from a generator function. With a generator,
    you *can* return successive elements of a list, but you use the 'yield'
    keyword instead of 'return', and repeated calls to the generator return
    each successive value.

    -- Paul
    Paul McGuire, Aug 16, 2005
    1. Advertisements

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. saoirse_79

    looping through a list of lists.

    saoirse_79, Oct 8, 2003, in forum: Python
    Oct 8, 2003
  2. Rob Hunter

    Re: looping through a list of lists.

    Rob Hunter, Oct 8, 2003, in forum: Python
    anton muhin
    Oct 8, 2003
  3. Jon Bowlas

    RE: looping list problem

    Jon Bowlas, Aug 16, 2005, in forum: Python
    Dennis Lee Bieber
    Aug 17, 2005
  4. Andy
    James Kanze
    Jun 8, 2007
  5. Replies:

Share This Page