How do I make a dictionary with multiple keys to one value?

dictionary, python

Solution

I guess you mean this:

class Value:
    def __init__(self, v=None):
        self.v = v

v1 = Value(1)
v2 = Value(2)

d = {'a': v1, 'b': v1, 'c': v2, 'd': v2}
d['a'].v += 1

d['b'].v == 2 # True

- Python's strings and numbers are immutable objects,

- So, if you want `d['a']` and `d['b']` to point to the same value that "updates" as it changes, make the value refer to a mutable object (user-defined class like above, or a `dict`, `list`, `set`).

- Then, when you modify the object at `d['a']`, `d['b']` changes at same time because they both point to same object.

Problem

I have a question about a dictionary I want to make. My goal is to have multiple keys to a single value, like below: ``` dictionary = {('a', 'b'): 1, ('c', 'd'): 2} assert dictionary['a'] == 1 assert dictionary['b'] == 1 ``` Any ideas?

Original source

Related problems