Dictionary

What's a dictionary in Python? {}

Another useful data type built into Python is the dictionary (see Mapping Types — dict). Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can’t use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend().

It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; this is also the way dictionaries are written on output.

The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated with that key is forgotten. It is an error to extract a value using a non-existent key.

The keys() method of a dictionary object returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just apply the sorted() function to it). To check whether a single key is in the dictionary, use the in keyword.

Here is a small example using a dictionary:

>>> tel = {'jack': 4098, 'sape': 4139}>>> tel['guido'] = 4127>>> tel{'sape': 4139, 'guido': 4127, 'jack': 4098}>>> tel['jack']4098>>> del tel['sape']>>> tel['irv'] = 4127>>> tel{'guido': 4127, 'irv': 4127, 'jack': 4098}>>> tel.keys()['guido', 'irv', 'jack']>>> 'guido' in telTrue

The dict() constructor builds dictionaries directly from lists of key-value pairs stored as tuples. When the pairs form a pattern, list comprehensions can compactly specify the key-value list.

>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]){'sape': 4139, 'jack': 4098, 'guido': 4127}>>> dict([(x, x**2) for x in (2, 4, 6)]) # use a list comprehension{2: 4, 4: 16, 6: 36}

Later in the tutorial, we will learn about Generator Expressions which are even better suited for the task of supplying key-values pairs to the dict() constructor.

When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments:

>>> dict(sape=4139, guido=4127, jack=4098){'sape': 4139, 'jack': 4098, 'guido': 4127}

(python.org)

And one of the very best elements in python are dictionarys. As I coded a lot in Mel, I often had to save different types of data. Lets say you have 100 characters, every one has a name, a position and a startFrame for a walk. In mel you had to do this:

Code:

string $name[100]; vector $pos[100]; float $startFrame[100];

Then you had to modify all these arrays. No fun at all. Now in a dictionary you can do this:

Code:

charDict = {} for i in range(100): charDict['charNamei'] = {'pos' : [x,y,z], 'startFrame' : 123.0}

And you can later access with

Code:

sf = charDict['myCharacter']['startFrame']

instead of walkig though several arrays. Of course, if the actions are a bit more complex, it is possible to create your own characer class.

(haggi)

My First Dictionary

awwwww

fingerLengths = {}

fingNumJts = []

for f in fingers:

childJts = mc.listRelatives(f, ad=1, type='joint')

fingNumJts.append(len(childJts))

fingerLengths = dict([(fingers[i], fingNumJts[i]) for i in range(len(fingers))])

print fingerLengths

While writing Python classes for a custom tool-set, I started using dictionaries to organize and initialize my data. A dictionary is simply an associative array. I use them to initialize custom data sets when I instance a class. Dictionaries come in real handy when building UI elements and collecting information. Here's an example...

import pymel.core as pm

class MyNewClass:

def __init__(self):

dict = {}

dict["dagObjects"] = [ [object] for object in pm.ls(dag = True) if object != someobject]

(3DevArtist)

More Dictionary Resources

https://www.geeksforgeeks.org/python-dictionary/

Rig Example

using for autorig:

https://groups.google.com/forum/#!topic/python_inside_maya/MdI0xVv2olY