3 Answers
3

The only way to store "multiple values" for a given key in a dictionary is to store some sort of container object as the value, such as a list or tuple. You can access a list or tuple by subscripting it, as you do in your first example.

The only problem with your example is that it's the ugly and inconvenient way to access such a container when it's being used in this way. Try it like this, and you'll probably be much happier:

As FabienAndre points out in a comment below, there's also the more convenient syntax I'd entirely forgotten about, for key,(posX,posY,posZ) in names.items():.

You don't specify where you're getting these values from, but if they're coming from code you have control over, and you can depend on using Python 2.6 or later, you might also look into named tuples. Then you could provide a named tuple as the dict value, and use the syntax pos.x, pos.y, etc. to access the values:

I would also mention for key,(posX,posY,posZ) in names.items():
–
FabienAndreSep 5 '10 at 0:16

I attempted to use your method, but kept getting Error: too many values to unpack. However, when I used your second example from FabienAndre, it worked perfectly. I think the first example didn't work because of how I'm getting my data in Maya, but I could be entirely wrong. Thanks to both of you!
–
ArrantSquidSep 5 '10 at 0:47

Yes I did and it's something that I seem to forget a lot as well - albeit I am much newer to this than you are so I forget a multitude of things. :) After changing it, I still seemed to have issues with it, but I believe it's because I'm cycling through a list. Not sure, but I've tried using the .append to no avail. :( However, the other example still works well and is explicit about what I'm trying to get, so I like it. :) Thanks again for the help! Much appreciated.
–
ArrantSquidSep 5 '10 at 1:10

I hadn't thought of doing something like this, but I may use that in other parts of my current code, since this seems a very good way to split things up, while still making sense.
–
ArrantSquidSep 5 '10 at 0:49

How to populate the object locations dynamically?
–
PunitDec 16 '14 at 11:49