Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple values for key in dictionary in Python

What I'm trying to do is get 3 values from a key into separate variables. Currently I'm doing it like this:

for key in names:
   posX = names[key][0]
   posY = names[key][1]
   posZ = names[key][2]

This doesn't seem very intuitive to me even though it works. I've also tried doing this:

for key, value in names:
   location = value

Unfortunately, this gives me a single object (which is what I expected), but I need the individual values assigned to the key. Thanks and apologize for my newness to Python.

Update Apologies for not specifying where I was getting my values from. Here is how I'm doing it for the first example.

names = {}

for name in objectNames:
    cmds.select(name)
    location = cmds.xform(q=True, ws=True, t=True)
    names[name] = location
like image 870
John P Avatar asked Sep 04 '10 23:09

John P


People also ask

Can a Python dictionary key have multiple values?

General Idea: In Python, if we want a dictionary to have multiple values for a single key, we need to store these values in their own container within the dictionary. To do so, we need to use a container as a value and add our multiple values to that container. Common containers are lists, tuples, and sets.

How do I store multiple values in one key in Python?

In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.

Can you have multiple values for one key?

Each key can only have one value. But the same value can occur more than once inside a Hash, while each key can occur only once.


1 Answers

It's not unintuitive at all.

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:

>>> alist = [1, 2, 3]
>>> one, two, three = alist
>>> one
1
>>> two
2
>>> three
3
>>> 

Thus your second example could instead be:

for key, value in names.items():
    posX, posY, posZ = value

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:

for name, pos in names.items():
    doSomethingWith(pos.x)
    doSomethingElseWith(pos.x, pos.y, pos.z)
like image 60
Nicholas Knight Avatar answered Nov 15 '22 16:11

Nicholas Knight