I am reading an element list from an xml file and make the data into 2 dictionaries.
Was this the fastest way? (I don’t think this is the best, you guys always surprise me.;-)
ADict = {}
BDict = {}
for x in fields:
key = x.get('key')
ADict[key] = x.find('A').text
BDict[key] = x.find('B').text
I think add it one by one is a bad idea, but write it in a single line. aka more pythonic way like this
ADict,BDict = [dict(k) for k in zip(*([(x.get('key'),x.find('A').text),(x.get('key'),x.find('B').text)] for x in fields))]
I don’t think it’s better, two reasons,
first, x.get('key') was called twice
second, create too much temp tuples
Not tested, but should work