1

I have a dictionary which defines the scheme for translating XML data by mapping certain keywords to certain XML element paths. The dictionary looks like this:

scheme = { 'phone' :  'People/Analyst/Phone', \
           'email' : 'People/Analyst/Email', \
           'address' : 'People/Analyst/Address' \
           ... and so on

I now have to accommodate a situation where a certain key might need to map to two or more element paths that will need to be concatenated. For example:

scheme = { 'phone' :  'People/Analyst/Phone', \
           'email' : 'People/Analyst/Email', \
           'address' : 'People/Analyst/Address' \
           'name' : [ 'People/Analyst/FirstName', 'People/Analyst/FirstName', ' ' ]
           ... and so on

I figured that a list as a value in the dictionary for that scheme is the right way to do this. But what's the idiomatic way to go through such a dictionary? As someone relatively new to Python, I'd like to start doing things a little more pythonically. Right now I have something like this:

for k in scheme:
  dataStore[k] = fetchXMLText(scheme[k])

But if an occasional value in scheme will be a list with two or more parameters and a trailing separator, what's the most idiomatic way to check for write this up? I imagine something like checking to see if scheme[k] is an instance of a list, and then going through that list individually. I can code that up easily, but is there some more elegant way than doing a conditional isinstance().... ?

Many thanks

5
  • 3
    Why not use a dictionary whose values are all lists? You could replace the string values in your current dictionary with single-element lists containing those same strings. That would make your iteration simpler. Commented Oct 4, 2013 at 15:05
  • Having all of the scheme values be lists is certainly one solution I've considered. Although I feel like if 95% of my scheme entires won't need to be lists, it adds a bit of unnecessary bloat.
    – rgb
    Commented Oct 4, 2013 at 15:08
  • 1
    I'd maintain that it's more "pythonic" to use a homogenous data structure than to explicitly check data types in this case. Commented Oct 4, 2013 at 15:11
  • 1
    Minor note: You can make your iteration a little nicer by iterating over the key/value pairs in the dictionary with mydict.iteritems(), instead of manually looking it up (as was done with scheme[k]).
    – Moshe
    Commented Oct 4, 2013 at 15:12
  • Ok! appreciate your advice
    – rgb
    Commented Oct 4, 2013 at 15:27

1 Answer 1

6

I agree with @EmmettJ.Butler. To turn that into a complete answer:

Say you had:

scheme = { 'phone'  : ['People/Analyst/Phone'], \
           'email'  : ['People/Analyst/Email'], \
           'address': ['People/Analyst/Address'] \
           'name'   : [ 'People/Analyst/FirstName', 'People/Analyst/FirstName', ' ' ]
           ...

You could then iterate it through as follows:

for k,v in scheme.iteritems():
  dataStore[k] = [fetchXMLText(path) for path in v]

Not the answer you're looking for? Browse other questions tagged or ask your own question.