I have a loop giving me three variables
matteGroup
matteName
object
I would like to make a nested dicionary holding all the data like:
if speed is a concern, you could utilize try/except clauses to just try to populate your data first rather than checking if items exist and then adding it each time through the loop
diz = {}
for obj in mc.ls(type='transform'):
try:
matteGroup = mc.getAttr('%s.matteGroup' %obj)
matteName = mc.getAttr('%s.matteName' %obj)
except Exception:
continue
try:
diz[matteGroup]
except KeyError:
diz[matteGroup] = {matteName : [obj]}
continue
try:
diz[matteGroup][matteName].append(obj)
except KeyError:
diz[matteGroup][matteName] = [obj]
for the first try/except, it would be best to put whatever exception maya throws if an attr doesn't exist on a node (don't have maya open right now, so I couldn't put that in...). This essentially checks for the attr and continues to the next obj if the attr isn't there. You can put them both in there instead of each having their own try/except, because it should error if either doesn't exist anyway.
the second try/except is checking if the matteGroup is in the top level of your dict. If it isn't, then you know the matteName and list of obj's isn't in your data structure either, so it adds them and continues to the next obj
the third try/except tries to append the obj to the matteName dict item's list. If you get a keyError here, it means that the matteName isn't in your matteGroup dict, so it then adds it and creates the list with the current obj as the first item in that list.
So, as far as speed is concerned, any time items exist in your data structure, the next obj you add to that data item will essentially just get added without having to test if all of the other data structure is in place before adding it, making your loop go faster the further into the loop you go (provided there are a lot of nodes sharing matteGroups and/or matteNames)