Can a tuple be used as a dictionary key?

Question

This exercise says a key can be a hashable type like a number or string. Can a tuple be used as a dictionary key?

Answer

Yes, a tuple is a hashable value and can be used as a dictionary key. A tuple would be useful as a key when storing values associated with a grid or some other coordinate type system. The following code example shows a dictionary with keys representing a simple x,y grid system.

coordinates = { (0,0) : 100, (1,1) : 200}
coordinates[(1,0)] = 150
coordinates[(0,1)] = 125

print(coordinates)
# {(0, 0): 100, (1, 1): 200, (1, 0): 150, (0, 1): 125}
9 Likes

Am i correct in assuming that the value being calculated is based on the binary system?

That means when using a Tuple as key, the dictionary can actually calculate stuff… cool.

2 Likes

No, you’re wrong
This example just shows that the author added some values manually to the dictionary by using tuples as keys of the values
Similar to .append() in lists

4 Likes

Oh God i’m stupid, you’re correct. That was just a simple key-value assignment. Thanks for correcting me.

& welcome to the forums btw! :smiley:

4 Likes

what if I have a dictionary like this :

my_dict = {(1,2): (12, 4), (3, 4): (13, 5), (5, 6): (14, 5)}

if I want to pair each value in the first tuple with the corresponding value in the second tuple, how would I do that?

I tried it and it seems to work exactly as you typed it :+1:

1 Like

I’m wondering that, too .Is there anyone in here to show how to do that pairing?

1 Like

As the user above posted you already can create a dictionary like this so that your key:value pair is a tuple mapping to a particular value. Could you explain what other kind of pairing you have in mind?

student_exam_result ={ (“Jane” , “Ashley”, “Celine”) : (80, 90, 65),
(“Tim”, “Emile”) : [65, 75) }
how can I reach just tim’s score. It’s what I meant when I’m saying pairing… Thank you for answering by the way.

1 Like

You’d first need to access the dictionary by key ("Tim", "Emile",) and then index the list or tuple you use next. So student_exam_result[("Tim", "Emile")][0] in this case. That would be a very unfortunate choice of nesting here, there is likely a much better way to organise such values if the use of this dictionary wass to get individual student scores back.

It’s clear now. Thank you for your answer.

1 Like

Good Question, and the example demonstrates the power of a dictionary. ty

If you look at what you’re asking, is that you want a mapping from (“Jane” , “Ashley”, “Celine”) to (80, 90, 65) so that Jane is associated with 80 etc. This is simply a dictionary! so instead the thing to do is {"Jane":80,"Ashley":90,"Celine":65}, which contains the much simpler and obvious mapping between name and score.

1 Like

What you are asking to do can be done many different ways using logic. Not the ‘right’ way to create dictionaries but can be done. I can think of a couple of other ways to do this but one of them is below. Meanwhile, since keys of a dictionary cannot be duplicated - if any of your values in each first tuple is a duplicate of another value which you are intending to use as a key - I have already made a provision for being able to give a new value. Of course, you could inadvertently enter a NEW value that is also a duplicate and that will require more logic to handle - which I have not written. Hopefully, this piece of code will suffice.

1 Like

A lot of things to learn here. Here’s another simple example that could help to get things more clear about using a tuple as a key in a Python dictionary :

coordinates_info = {}

Defining a tuple representing coordinates (latitude, longitude)

location_1 = (34.0522, -118.2437)
coordinates_info[location_1] = “Los Angeles”

Adding another location

location_2 = (40.7128, -74.0060)
coordinates_info[location_2] = “New York City”

print(coordinates_info)

{(34.0522, -118.2437): ‘Los Angeles’, (40.7128, -74.0060): ‘New York City’}

1 Like