Use the get function and provide a default value where necessary.  Clearing
out stuff would just be adding time to your processing.

foo = mydict.get(key, None)

Do that once then use the variable instead of getting the key again. No
need to over complicate your code
On Oct 30, 2013 4:41 AM, "Simen Chris" <[email protected]> wrote:

>
>
> Hey guys, I often use dictionary in my functions to store and return
> created elements, however, it is really a pain to deal with if something in
> the dictionary doesn't exist. So I would like to have a function that goes
> through all of the elements in a dictionary, and if it finds object's that
> doesn't exists it should remove the values from the dictionary, and if it
> finds empty keys, it should delete them also.
>
> Here's what I got so far:
> #This is my dictionary
> myDict['objects']:  [('pSphere1'),
>                      ('pSphere2'),
>                     [('pCube1'), ('pCube2'), ('pCube3')],
>                     [[('loc1'), ('loc2')], [('obj1'), ('obj2')]],
> myDict['nodes']:    [('pointConstraint1'),
>                      ('pointConstraint2'),
>                      ('pointConstraint3'),
>
> #I want to go through each item in the dictionary
> for key, val in myDict.iteritems():
>     #If the current value is a list
>     if isinstance(val, list) == True:
>         #For each item in the list
>         for valLoop1 in val:
>             #If the current value is a list
>             if isinstance(valLoop1, list) == True:
>                 #For each item in the list
>                 for valLoop2 in valLoop1:
>                   #If the current value is a list
>                   if isinstance(valLoop2, list) == True:
>                     #And this goes on and on
>                   #If the current item is not a list
>                   else:
>                     #If the object doesn't exist
>                     if not cmds.objExists(val):
>                       #Pop the value out from the dictionary
>                       #Don't know how to do this yet
>             #If the current item is not a list
>             else:
>               #If the object doesn't exist
>               if not cmds.objExists(val):
>                 #Pop the value out from the dictionary
>                 #Don't know how to do this yet
>     #If the current item is not a list
>     else:
>       #If the object doesn't exist
>       if not cmds.objExists(val):
>         #Pop the value out from the dictionary
>         #Don't know how to do this yet
>
> http://pastebin.com/2Q6dzLut
>
> This is pretty ugly, how many lists it supports is dependant on how many
> isinstances I write.. Also if I find an object that doesn't exist, I don't
> know how to pop that specific value from the dictionary, and also if it
> finds empty keys..
>
>
> Does anyone have any tips or hints on this?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/df9f7cba-cc97-417e-b084-da18c99a2485%40googlegroups.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAM9RXo%2BQa2ZOD3OqQCPd9f%3Dw-kPnwbrOHXnfeEt2C%3DxdKijeyQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to