Building/Exporting Lists

I’m reading data from an XML file and would like to export in a format that I cannot get to work. See script and picture below. I would like to have all the “Smarttag” information under one list for each Clash. It is corrently repeating lists for each Clash.



Sorry it’s hard to know exactly without some test files…

But I suspect that you need to play with the depth where you create a List and the depth where you append the Variable…

Here’s me messing around to show what I’m talking about…

Hope that helps,


Agreed with @Mark.Ackerley, please provide some test files so the community can use that test file and generate a best solution for your use case.
If i never interpret wrongly, you just want all your key pair values(sorry thats how C# calls it) to be consolidated into one dict. So based on the limited information you had given us, your code should be like this:

import sys
sys.path.append("C:\Program Files (x86)\IronPython 2.7\Lib")

import xml.etree.ElementTree as ET
dataIN = IN[0]
tree = ET.parse(dataIN)
root = tree.getroot()

finalDict = dict()

for clashresult in root.iter('clashresult'):
	name = clashresult.get('name')
	for clashobject in clashresult.iter('clashobject'):
		for obj in clashobject.iter('smarttag'):
			smarttag = [x.text for x in obj]
			finalDict[name] = smarttag
OUT = finalDict

or if you prefer a one liner:

OUT = {clashresult.get('name'):[x.text for x in obj] for clashresult in root.iter('clashresult') for clashobject in clashresult.iter('clashobject') for obj in clashobject.iter('smarttag')}

Well of course these codes couldnt be tested as their werent any sample xml file for me to test but here you go. hope it helps

1 Like