Can I have a variable in my dictionary in python?

Is it possible to have the key or the value in a python dictionary be an outside variable?

for example:

a = 50
b = 60

new_dict = {a: "hello", b: "hi"}

I’m not sure one would even want to do this but I was curious.

Hi @dev6623677446 , you can try out:

a = 50
b = 60

new_dict = {a: "hello", b: "hi"}
print(new_dict)

Result:

{50: 'hello', 60: 'hi'}

The question is, what if we change the values of a and/or b?

a = 60
b = 70
print(new_dict)

Result:

{50: 'hello', 60: 'hi'}

We see the same result.
When we assign the keys with variables, only the values are used during the dictionary creation.
The key must be hashable. This means it is immutable : static and cannot be modified .

2 Likes

Yes, this would be a fairly common usage. But as @tezza_chen mentions it is not a which is stored as a key but the object a references. If you’re unfamiliar with it then it might be worth looking into mutable and immutable objects in python (especially if you are familiar with the concept of “variables” from other languages).

Some folks make an analogy of names being labels you stick on objects, e.g. you can add several labels to the same object you can take your label off and stick it on another object and so on).

For a very short intro you can treat a like a label which references the actual object, in this case an integer with a value of 50. Integer objects in Python are immutable (they cannot be changed). You can change your label a to reference something else but you cannot directly change the integer object itself. A quick('ish) example-

a = 50
b = a  # b now references the same object as a
print(b)
Out: 50
a = 60  # change the *object* a references
print(b)  # has b changed?
Out: 50  # no, it still references the first object

Contrast this with a list which does have methods to mutate the object (methods like .append, .pop, .sort and so on). If you can separate the name or identifier a from the object it references then Python will make start making more sense.

So back to the dictionaries, your key uses that first object, integer 50.The key itself is hashable, an integer object with a value of 50 that cannot be changed. It doesn’t matter then what you do with the label a; the dictionary uses that first object as a reference.

To take this a step further so it’s clear, keys which aren’t valid would throw an error when creating the dictionary. The following example would throw an error as your list object could be modified and the key contained by the dictionary itself would be altered which would be very undesirable-

a = [50]
new_dict = {a: "hello"}
TypeError: Unhashable type: 'list'

The comment about hashable types being immutable is that you can’t use mutable types in the first place.

2 Likes

@tgrtim gives an excellent explanation of mutable and non-mutable objects and what this means in terms of referencing values. If you’re interested in what the term hashable means (this was a long-standing source of confusion for me), here is a good blog post that explains it: https://betterprogramming.pub/3-essential-questions-about-hashable-in-python-33e981042bcb

1 Like