Dictionary keys in Python are unique. Python will resolve D = {'a':1,'a':2} as D = {'a': 2}

You can effectively store multiple values under the same key by storing a list under that key. In your case,

D = {'a': [1, 2]}

This would allow you to access the elements of 'a' by using

D['a'][elementIdx]   # D['a'][0] = 1
Answer from schwadan on Stack Overflow
🌐
Quora
quora.com › How-do-I-access-the-dictionary-if-we-have-the-same-keys-with-different-values-in-Python
How to access the dictionary if we have the same keys with different values in Python - Quora
Answer (1 of 4): One of the property of python dict key is unique... So in python dictionary key should be unique ... but you can able to define duplicate at run time... once your dict is stored the duplicate will automatically removed or it will update . In run time you can create like this, ...
Discussions

Can a dictionary have two keys of the same value?
Question Can a dictionary have two keys with the same value? Answer 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 ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
7
August 4, 2018
python - Dictionary (same value, different key) - Stack Overflow
Explore Stack Internal ... I'm new to Python and when I'm basically adding values to a dict, I find that when I'm printing the whole dictionary, I get the same value of something for all keys of a specific key. ... Basically with every end element of "row", I'm storing the element by it's key: ... More on stackoverflow.com
🌐 stackoverflow.com
Dictionary: Same Key, Different Values
You can't, because the initial dictionary is not possible. A dictionary always maps one key to one, and no more than one, value. More on reddit.com
🌐 r/learnpython
8
0
December 22, 2020
How to find the keys with same values in a dictionary?
Assemble a new dictionary with the values from the initial dictionary as keys for the second. The original's keys would grow the list associated with the keys in the new dict. data = { "a": 5, "b": 7, "c": 5, "d": 7, } frequencies = {} for key, value in data.items(): if not value in frequencies: frequencies[value] = [key] # we want the new dict to use lists for values else: frequencies[value].append(key) # if the list exists, use append for the new value The result: {5: ['a', 'c'], 7: ['b', 'd']}. At this point, identifying keys with similar values just comes down to checking the length of the lists for each key in the new dictionary. More on reddit.com
🌐 r/learnpython
13
3
March 16, 2022
🌐
EyeHunts
tutorial.eyehunts.com › home › python dictionary same key multiple values | example code
Python dictionary same key multiple values | Example code
October 17, 2023 - Just associate the list object to a key if you want the same key to have multiple values in the Python dictionary. A list object will have multiple elements. ... Simple example code where multiple values in a list correspond to the key so that ...
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
Can a dictionary have two keys of the same value? - Python FAQ - Codecademy Forums
August 4, 2018 - Question Can a dictionary have two keys with the same value? Answer 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.
🌐
Quora
quora.com › Can-two-dictionaries-have-the-same-keys-but-different-values
Can two dictionaries have the same keys but different values? - Quora
Answer: If you mean dictionaries in Python, then yes, certainly! A dictionary has immutable keys but mutable values, i. e., you can change the value of a key, but can't assign two or more different values to a particular key simultaneously. But, if there are two dictionaries with the same key &...
Find elsewhere
🌐
EyeHunts
tutorial.eyehunts.com › home › how to add the same key value in dictionary python | example code
How to add the same key value in dictionary Python | Code
September 14, 2023 - # Initialize a dictionary my_dict ... want to have multiple values associated with the same key, you can use a list or another data structure as the value....
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-add-same-key-value-in-dictionary-python
How to Add Same Key Value in Dictionary Python - GeeksforGeeks
July 23, 2025 - In this example, we will use a Python list of values as the value of the keys of the Dictionaries. We will convert the value of the same key in a single list and then use that list as the value of that key.
🌐
Reddit
reddit.com › r/learnpython › how to find the keys with same values in a dictionary?
r/learnpython on Reddit: How to find the keys with same values in a dictionary?
March 16, 2022 -

My dictionary is

G[i].the_Color_Of_Neighbours[vertex] = color_number_of_neighbours

So , the dictionary holds a particular vertex and the color numbers of it's neighbours which is a list. The dictionary looks something like

{ Vertex1 : [1,2,3] , Vertex2: [5,6,1], Vertex3: [6,4,1] ......... }

I would like to store all the vertex's with the same color_number_of_neighbours maybe in another list. So that list could look like

[ [Vertex1, Vertex 4, Vertex 5] , [Vertex7, Vertex6, Vertex0], [Vertex10, Vertex11] ]

This could happen if Vertex1, Vertex4 and Vertex 5 have the same color_number_of_neighbours. And, Vertex7, Vertex6 and Vertex0 have the same color_number_of_neighbours. Vertex10 and Vertex11 have the same color_number_of_neighbours. Also the order of the numbers in the list don't matter, the numbers in the list should just be the same

🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch01s06.html
Associating Multiple Values with Each Key in a Dictionary - Python Cookbook [Book]
July 19, 2002 - A normal dictionary performs a simple mapping of a key to a value. This recipe shows two easy, efficient ways to achieve a mapping of each key to multiple values. The semantics of the two approaches differ slightly but importantly in how they deal with duplication.
Authors   Alex MartelliDavid Ascher
Published   2002
Pages   608
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
Can a dictionary repeat identical keys or values? - Python FAQ - Codecademy Forums
October 24, 2019 - Question, What happens in a dictionary if you have repeated keys or repeated values? Answer Each of these is a separate case. Multiple keys can all have the same value as demonstrated below. This causes no issues.
Top answer
1 of 2
2
b) In Python, a dictionary can have two same values with different keysExplanationa) In Python, a dictionary can have two same keys with different values.Day = {1: 'monday', 1: 'tuesday', 3: 'wednesday'}print(Day)Output{1: 'tuesday', 3: 'wednesday'}Expected Output{1: 'monday', 1: 'tuesday', 3: 'wednesday'}So, we can conclude that this statement is not correct and the dictionary requires unique keys. Also, here the key '1' is getting overwritten by a new value 'tuesday'.b) In Python, a dictionary can have two same values with different keysDay = {1: 'monday', 2: 'monday', 3: 'wednesday'}print(Day)Output{1: 'monday', 2: 'monday', 3: 'wednesday'}Expected Output{1: 'monday', 2: 'monday', 3: 'wednesday'}So, we can conclude that this statement is correct and the dictionary can have two same values with different keysc) In Python, a dictionary can have two same keys or same values but cannot have two same key-value pairDay = {1: 'monday', 1: 'monday', 3: 'wednesday'}print(Day)Output{1: 'monday', 3: 'wednesday'}Expected Output{1: 'monday', 1: 'monday', 3: 'wednesday'}So, we can conclude that this statement is not correct as the dictionary can have the same values but, not same keys and also cannot have two same key-value paird) In Python, a dictionary can neither have two same keys nor two same values.Day = {1: 'monday', 1: 'tuesday', 3: 'wednesday'}Day1 = {1: 'monday', 2: 'monday', 3: 'wednesday'}print(Day)print(Day1)Output{1: 'tuesday', 3: 'wednesday'}{1: 'monday', 2: 'monday', 3: 'wednesday'}Expected Output{1: 'monday', 1: 'tuesday', 3: 'wednesday'}{1: 'monday', 2: 'monday', 3: 'wednesday'}So, we can conclude that this statement is not correct as the dictionary can have two same values but, cannot have two same keys.Extra InformationDictionary in Python is used to store data values in an unordered collection. For storing data in a dictionary keys are used. It's like a key: value pair in which data is held by unique keys.In this Day = {1: 'monday', 2: 'tuesday', 3: 'wednesday'}Day is a dictionary that has keys 1, 2 and 3 and its values are monday, tuesday, and wednesday respectively.Exampled1 = {"abc":5,"def":6,"ghi":7}d1 is a dictionary that has keys abc, def, and ghi and its values are 5, 6, and 7 respectively.Python is created by Guido van Rossum and came into existence in 1991. It is a high-level and general programming language. It is a very lucid programming language as it has a good language construct and object-oriented approach. It is dynamically typed and garbage-collected.
2 of 2
2
b)Explanation:In python, a dictionary can have two same values with different keys
Top answer
1 of 2
5

What you are trying to do is not possible with dictionaries. In fact, it is contrary to the whole idea behind dictionaries.

Also, your Sets class won't help you, as it effectively gives each name a new (sort of random) hash code, making it difficult to retrieve items from the dictionary, other than checking all the items, which defeats the purpose of the dict. You can not do dict.get(Sets(some_name)), as this will create a new Sets object, having a different hash code than the one already in the dictionary!

What you can do instead is:

  1. Just create a list of (name, value) pairs, or

    pairs = zip(names, values) # or list(zip(...)) in Python 3
    
  2. create a dictionary mapping names to lists of values.

    dictionary = {}
    for n, v in zip(names, values):
        dictionary.setdefault(n, []).append(v)
    

The first approach, using lists of tuples, will have linear lookup time (you basically have to check all the entries), but the second one, a dict mapping to lists, is as close as you can get to "multi-key-dicts" and should serve your purposes well. To access the values per key, do this:

for key, values in dictionary.iteritems():
    for value in values:
        print key, value
2 of 2
2

Instead of wanting multiple keys with the same name, could you getting away of having multiple values per each key?

names = [1]
values = [[1, 2, 3], [4, 5, 6]]

dict = {}

for i in names:
    dict[i] = values

for k,v in dict.items():
    for v in dict[k]:
        print("key: {} :: v: {}".format(k, v))

Output:

key: 1 :: v: [1, 2, 3]
key: 1 :: v: [4, 5, 6]

Then you would access each value like this (or in a loop):

print("Key 1 value 1: {}".format(dict[1][0]))
print("Key 1 value 2: {}".format(dict[1][1]))
Top answer
1 of 6
2

You can use set.difference within a dict comprehension:

In [29]: Result_one_two = {k: set(Dict_one[k]).difference(Dict_two[k]) for k in Dict_one}

In [30]: Result_one_two
Out[30]: {'A': {'a2', 'a3'}, 'B': set(), 'C': {'c4'}}

In [31]: Result_two_one = {k: set(Dict_two[k]).difference(Dict_one[k]) for k in Dict_one}

In [32]: Result_two_one
Out[32]: {'A': set(), 'B': set(), 'C': {'c5'}}

It's better to preserve the values as set at the first place tho. In that case you won't need to call the set for each value. Also note that if you're using Python-3.6+ since dictionaries in these versions are insertion ordered the output will be as expected, otherwise you should use an OrderedDict to keep track of orders. But if the performance is not an issue here and/or you're dealing with a short data set you can just use a list comprehension as is explained in other answers.

Also, as mentioned in comments, if the order of values is important for you in order to take advantage of set operations and yet keep the order you can use a custom ordered set like what's proposed here https://stackoverflow.com/a/10006674/2867928 by Raymond Hettinger.

2 of 6
1

You can do so:

Result_one_two = {
    k: [v for v in vals if v not in Dict_two.get(k, [])]
    for k, vals in Dict_one.items()
}

Output:

Result_one_two = {'A': ['a2', 'a3'], 'C': ['c4'], 'B': []}

And second one:

Result_two_one = {
    k: [v for v in vals if v not in Dict_one.get(k, [])]
    for k, vals in Dict_two.items()
}

Output:

Result_two_one = {'A': [], 'C': ['c5'], 'B': []}
🌐
TutorialsPoint
tutorialspoint.com › article › in-the-python-dictionary-can-one-key-hold-more-than-one-value
In the Python dictionary, can one key hold more than one value?
March 27, 2026 - A Python dictionary stores key-value pairs where each key maps to a single value. However, you can store multiple values for one key by using containers like lists, tuples, or sets as the value.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-add-duplicate-keys-in-dictionary-python
How to Add Duplicate Keys in Dictionary - Python - GeeksforGeeks
July 23, 2025 - If the key doesn't exist, it is created with an empty list. If we require true duplicate keys, storing each key-value pair as a separate dictionary inside a list allows multiple entries for the same key.