Answers for "how to use multiple keys for single value in dictionary python"

2

how to have multiple values to a key in dict in python

from collections import defaultdict
 data = [(2010, 2), (2009, 4), (1989, 8), (2009, 7)]
 d = defaultdict(list)
print (d) # output --> defaultdict(<type 'list'>, {})
 for year, month in data:
     d[year].append(month) 
print (d) # output --> defaultdict(<type 'list'>, {2009: [4, 7], 2010: [2], 1989: [8]})
Posted by: Guest on September-23-2020
0

how to use multiple keys for single value in dictionary python

#Python Dictionary only contain one key : one value pair.
#You can surely have multiple keys having same value
#Example :-
  dict = {'a':'value1', 'b':'value2', 'c':'value 3'}
#In above we have a and b key having same values
Posted by: Guest on March-15-2021
1

python dictionary multiple same keys

No, each key in a dictionary should be unique. 
You can’t have two keys with the same value. 
Attempting to use the same key again will just overwrite the previous value stored. 
If a key needs to store multiple values, 
then the value associated with the key should be a list or another dictionary.
Sourece: https://discuss.codecademy.com/t/can-a-dictionary-have-two-keys-of-the-same-value/351465
Posted by: Guest on February-15-2021

Code answers related to "how to use multiple keys for single value in dictionary python"

Python Answers by Framework

Browse Popular Code Answers by Language