Adding keys and values to dictionary [Python]

Hey Guys, so trying to add keys and values to a dictionary.

The Result that it keeps giving me is just the last pairing of keys and values.
{‘UpperLid_Open’: ‘UpperLid_Open_InBtwn090’}

Shouldn’t it be updating the whole list?
Why is is it only spitting out the last pairing of keys and values?

I am using Maya 2012 with Python version 2.6.4 if that is of any help.

Here is my code

# Select the blendshapes then target
args = cmds.ls(sl=True)
blends = cmds.ls(args[:-1])

# Find all in between blendshapes in the scene
allIBs=cmds.ls('*_InBtwn0*', type ='transform')

# 
keys = []
values = []
for blend in blends:
    for IB in allIBs:
        if blend in IB:
            keys.append(blend)
            values.append(IB)


dictionary = {}
keysAndValues = zip(keys, values)
for key, value in keysAndValues:
    dictionary[key] = value

# Isnt this the same as above??
dictionary = dict(zip(keys, values))

Those should have the same result. Do they not? Are you sure there are more values in the keys and values list. Can you provide a print out of the keys and values

Keys are on the top and values on the bottom.

# Result: ['UpperLid_Open',
 'UpperLid_Open',
 'UpperLid_Open',
 'UpperLid_Open',
 'UpperLid_Open',
 'UpperLid_Open',
 'UpperLid_Open',
 'UpperLid_Open',
 'UpperLid_Open'] # 
values
# Result: ['UpperLid_Open_InBtwn010',
 'UpperLid_Open_InBtwn020',
 'UpperLid_Open_InBtwn030',
 'UpperLid_Open_InBtwn040',
 'UpperLid_Open_InBtwn050',
 'UpperLid_Open_InBtwn060',
 'UpperLid_Open_InBtwn070',
 'UpperLid_Open_InBtwn080',
 'UpperLid_Open_InBtwn090'] #

Python dictionaries can only have one entry per key. It looks like you want the value of each blendshape key to be a list.

from collections import defaultdict
# Select the blendshapes then target
args = cmds.ls(sl=True)
blends = cmds.ls(args[:-1])

# Find all in between blendshapes in the scene
allIBs=cmds.ls('*_InBtwn0*', type ='transform')

# 
inbetweens = defaultdict(list)
for blend in blends:
    for IB in allIBs:
        if blend in IB:
            inbetweens[blend].append(IB)
1 Like

Thanks! Quick question though.

So if i had a bunch of different keys [a, b, c, d] and values [123, 4554, 34, 66654545] the original way that I had it would have been the way to go?

if you want to combine two separate lists of keys and values into a dictionary, dict(zip(keys, values)) is the way to go.

1 Like

Cool thank you!