11

I'm adding into the value section of a dictionary a name and the value of properties of an item:

value+=child.get('name') + '\t' + child.text + '\t' 

Each piece of text is separated by a tab. So then when I process this value string later, I split on the tabs and have a for loop to iterate over the list.

How can I access both the name and value in the for loop. As for each property I want to get the name and value in one go and write it to a file. Currently the list looks like:

[a,b,a,b,a,b,a,b]

and for each a,b I want to write:

'<' tag name=a '>' b '<' /tag '>'

charlie123
  • 253
  • 1
  • 5
  • 17

3 Answers3

21

You can iterate over the list with step size 2, and get the name and tag each over each iteration...

for i in range(0,len(list1),2):
    name = list1[i]
    tag = list1[i+1]
    print '<tag name="%s">%s</tag>' % (name, tag)
jmetz
  • 12,144
  • 3
  • 30
  • 41
4

Edit: If your keys are unique, and the ordering doesn't matter, then...

I think you should convert your list to a dictionary, and then iterate over the keys of the dictionary:

# assuming you converted your list to a dictionary called values

for k in values:
   print '<tag name="%s">%s</tag>' % (k, values[k])

Edit: If you don't need the dictionary for anything but printing out the result, then the other answer that was posted is probably a better method.

Community
  • 1
  • 1
Gordon Bailey
  • 3,881
  • 20
  • 28
  • If they naturally go together in some way other than name:value, as is typical for a dictionary mapping, you may wish to store them as tuples. – algorowara Aug 01 '12 at 17:07
  • While I tend to agree with you overall that a `dict` is a nice solution, the conversion in this case is non-trivial. The OP would need to take care when populating the `dict` to check if a key already exists - instead of overwriting it they would have to append to the value. – jmetz Aug 01 '12 at 17:07
  • These are all good points, I'll edit to reflect them. – Gordon Bailey Aug 01 '12 at 17:09
  • 1
    Don't use values.keys(), just use values – Snakes and Coffee Aug 01 '12 at 17:11
4

First off, using string += string2 is a bad idea, as each time it copies to a new string.

value+=child.get('name') + '\t' + child.text + '\t'

should be

values = ((child.get('name'),child.text) for child in children)

then when you print, simply do

for name,text in values:
    print '<tag name="{name}">{text}</tag>'.format(name=name,text=text)

if for some reason, you really want the tabs, then you will have to change the values constructor to a list (from a generator) and do:

''.join((name+'\t'+value+'\t' for name,value in values))[:-1]
Snakes and Coffee
  • 8,747
  • 4
  • 40
  • 60
  • While my answer answers the question most directly, I think this is the best (and most pythonic) approach so far if the OP doesn't mind switching away from the list approach. – jmetz Aug 01 '12 at 17:10
  • Agreed, using tabs as a delimiter that you then split on elsewhere makes no sense at all, this is a much cleaner/better approach. – Gordon Bailey Aug 01 '12 at 17:14