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:{}})
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)
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'}
How can I create a dictionary where the key is a tuple?
dict={} dict.update({value:{}})
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)
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'}