How can I create a dictionary where the key is a tuple

Question:

How can I create a dictionary where the key is a tuple?

dict={} dict.update({value:{}})

Asked By: string

||

Answers:

Based on the variable that you tried, you just define a key and value and use ‘update’ on the dictionary.

dict = {}
key = (1, 2)
value = "hi"
dict.update({key: value})
print(dict)
Answered By: iohans

Just like you’d do for any other key

tup1 = ('a', 'b', 'c', 'd', 'e')
tup2 = (1, 2, 3, 4, 5)
tup3 = ('A', 'B', 'C', 'D', 'E')

dict = {}
dict[tup1] = 'lowercase'
dict[tup2] = 'numbers'
dict[tup3] = 'uppercase'
print(dict)

{('a', 'b', 'c', 'd', 'e'): 'lowercase', (1, 2, 3, 4, 5): 'numbers', ('A', 'B', 'C', 'D', 'E'): 'uppercase'}
Answered By: NYC Coder
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.