Blog of


APyTA_07: Smart collections, advanced features (tuples and dicionnaries)

APyTA_07: Smart collections, advanced features (tuples and dictionaries)

In Python you can deal with an another variant of list. Please welcome TUPLES.
Tuples are size-fixed lists, with no possibility to assign items individually, and they are constructed with parenthesis():

myTuple= ("theName",12.34)
print type(myTuple), myTuple #<type 'tuple'> ('theName', 12.34)
myTuple[1]=42 # Error: TypeError: 'tuple' object does not support item assignment #
myTuple = ("otherName",2)
print myTuple #myTuple = ("otherName",2)

So when to use it ? As they are less flexible than lists, tuples have better performance for a find loop for example.
Use them when you don't need to append/del (so always a same length of items), and when every change needs to update the whole collection.

Basically, I use tuples (not very often) when I have to return not an object but a two/three fixed elements related together.
Sometimes you can use tuples like a very small class (like a struct in c++),with a useful usage to give arguments set
or to give a return value (a single object). But be very careful at the order and the length when parsing it.

In the following example, I use tuple to store the mousePosition better than a list:

import pymel.core
# Procedure called on mousse left-button pressed
def SampleContextPress():
  pressPosition = pymel.core.draggerContext( 'sampleContext', query=True, anchorPoint=True)
  print type(pressPosition) , pressPosition #<type 'list'> [290.0, 650.0, 0.0]
  myTuple = ( pressPosition[0],pressPosition[1] )
  print myTuple #(290.0, 650.0)
  return myTuple

#Commands to link left-clic to our function
pymel.core.draggerContext( 'sampleContext', pressCommand='SampleContextPress()', cursor='hand' );

They are unordered list of key:value pairs, where a key is an immutable object (often a string, or a number, but can be a complex object).

Unlike Tuples, I use dictionnaries aaaaaall the time. They are constructed with braces {}.
Think about them as a fashion to tag objects, and make really smart (and powerful) collections and relations.
You can swap a lot of iteration time, parsing your list at the research of your "suitable" object when computing.

For a real-world example, when dealing with graphic interfaces, users selects widgets by their names right ?
But into your code, you have to store the full object (the transform node for example), for better flexibility and readability.
So in my logic function I will convert from the ObjectName #<type 'str'> into the Object itself #<class 'pymel.core.nodetypes.Transform'> !

import maya.cmds
#Scene Creation
myCubeTransform = pymel.core.modeling.polyCube()[0] #Remember APyTA05 ? first item of polyCube's return value
myTorusTransform = pymel.core.modeling.polyTorus()[0]
myPlaneTransform = pymel.core.modeling.polyPlane()[0]

myDict[myCubeTransform._name] = myCubeTransform #Link: name => object
myDict[myTorusTransform._name] = myTorusTransform
myDict[myPlaneTransform._name] = myPlaneTransform
print myDict #{u'pCube1': nt.Transform(u'pCube1'), u'pTorus1': nt.Transform(u'pTorus1'), u'pPlane1': nt.Transform(u'pPlane1')}

def toggleVisibility(_Name):
  if myDict[_Name].isVisible():

def UI_Prompt():
  cmds.window( width=150 )
  cmds.columnLayout( adjustableColumn=True )
  t = maya.cmds.text(l='Toggle Visibility of:')
  for currentKey in myDict.keys(): #Note here how I can easily acess to a list of keys
    maya.cmds.button(l=currentKey, c="toggleVisibility( '"+currentKey+"' )" ) #Notice the dual usage of simple/double quote (avoiding crappy escaping)


To finish with the dictionaries, you can also use list methods like len(), in, to valid your future access.
You also can have a MyDict.keys(), which is a list of string, containing the list of all the dictionaries keys.
Or you can also prompt the dictionary to valid your potential key with the MyDict.has_key method.

A complete understanding on how dictionaries work needs additional notions of HashTables.
To understand the hidden power behind that, I can refer you to this well written post: at

At last, a third type of collections exist, the SET.
Just remember that a set is a list with NO DUPLICATES.
A very direct way to convert is by doing that:

mySet = set(myList)
print type(mySet), mySet #<type 'set'> set([False, 'anotherCube', 42, 'aCube'])
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.