Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > looping list problem

Thread Tools

looping list problem

Jon Bowlas
Posts: n/a
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_additional_name_color>section_subheader_white </ucl_additional_name_colo
<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.


Reply With Quote
Paul McGuire
Posts: n/a
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

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
looping in array vs looping in a dic Python 5 09-20-2012 11:58 PM
std::list: remove from front without deleting or looping through whole list Andy C++ 3 06-08-2007 09:02 PM
RE: looping list problem Jon Bowlas Python 4 08-17-2005 12:51 AM
Re: looping through a list of lists. Rob Hunter Python 2 10-08-2003 05:31 PM
looping through a list of lists. saoirse_79 Python 0 10-08-2003 03:04 PM