Skip to content
Advertisement

Is there a way to find a value in a dictionary if the key has multiple values?

So for example:

a = {}


class Person:
    def getName(self):
        return self.name

    def getValue(self):
        return self.value

    def __init__(self, name, value):
        self.name = name
        self.value = value


oject = Animal("foo", 2)
a[oject.getName()] = oject.getValue(), oject, oject.getName()
valuee = 2

How would I look for valuee in the dictionary a? I have looked at other articles but most are unhelpful so I thought to turn to this website. Are there any other details I need to add to this post?

Advertisement

Answer

Your question would be likelier to get helpful answers if you took the trouble to post working code, and if it were clear from the title of your question what you are trying to do. Your title says you want to find a value but your code is assigning a value.

class Person:
    def getName(self):
        return self.name

    def getValue(self):
        return self.value

    def __init__(self, name, value):
        self.name = name
        self.value = value

class Animal(Person):    #without this your assignment to oject will fail
    pass

oject = Animal("foo", 2)
a = vars(oject)
print(a)

Output:

{'name': 'foo', 'value': 2}

Now this answer may be not what you meant at all. If not, try editing your question to explain whether you are trying to find values in a dictionary or assign them to a dictionary, ensure that the code you present actually works, and say clearly what output you expect.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement