I want to do something like this:
my_dict = {"key": "value", "key1": "value1", "key2": my_dict["key"]}
and have the result be:
{"key": "value", "key1": "value1", "key2": "value"}
Currently getting unresolved reference unless I declare the dict() prior. Otherwise I get a key error.
You can not have duplicate keys in Python, but you can have multiple values associated with a key in Python. If you want to keep duplicate keys in a dictionary, you have two or more different values that you want to associate with same key in dictionary.
[C#] Dictionary with duplicate keys The Key value of a Dictionary is unique and doesn't let you add a duplicate key entry. To accomplish the need of duplicates keys, i used a List of type KeyValuePair<> .
Python dictionary doesn't allow key to be repeated.
Dictionary in python are ordered, changeable, and do not allow duplicates. That means the dictionary cannot have two items with the same key; hence, dictionary keys are immutable.
Set the value of key2
to something phony (say, None
), initialize my_dict
properly, and then change the value of my_dict['key2']
to my_dict["key"]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With