0

I am looking to remove duplicates in a python dictionary but only where the keys are the same. Here is an example.

original_dict = {'question a' : 'pizza', 'question b' : 'apple', 'question a': 'banana'}

I want to remove the 'question a' item so there would only be one 'question a'. The problem I am facing is that the values are not the same. Any way to do this easily in Python 3.x?

Hudson
  • 29
  • 5

2 Answers2

4

By definition, the dictionary keeps only 1 value per key, so you will not have duplicates. In your example, the last value for the duplicate key is the one that will be kept (that's what "the old value associated with that key is forgotten" means below):

original_dict = {'question a' : 'pizza', 'question b' : 'apple', 'question a': 'banana'}
print(original_dict)
# {'question a': 'banana', 'question b': 'apple'}

From the docs:

It is best to think of a dictionary as a set of key: value pairs, with the requirement that the keys are unique (within one dictionary). [...] If you store using a key that is already in use, the old value associated with that key is forgotten.

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
  • While the documentation does indeed say there will only be one, it doesn't specify which one. – Mark Ransom Jul 15 '21 at 19:34
  • @MarkRansom Thank you for the comment. Updated the answer with another quote from the docs. – Timur Shtatland Jul 15 '21 at 19:39
  • Is there another part of the docs that specifies that the order of a definition will be from left to right? Probably so, since there's now a guarantee that iteration order will match insertion order. – Mark Ransom Jul 15 '21 at 19:43
  • @MarkRansom This is a good point. Unfortunately, I could not find that part in the docs, but just assumed that the iteration order will go left to right. – Timur Shtatland Jul 15 '21 at 19:48
  • 1
    Yes that is correct! I was trying to solve a problem that wasn't there haha. – Hudson Jul 15 '21 at 19:54
  • 1
    Well I'm curious now so I posted it as a question: https://stackoverflow.com/q/68400435/5987 – Mark Ransom Jul 15 '21 at 20:33
  • And of course my question was immediately closed as a duplicate of https://stackoverflow.com/questions/63873066/do-python-dict-literals-and-dictlist-of-pairs-keep-their-key-order – Mark Ransom Jul 15 '21 at 21:02
2

Python dictionary won't allow duplicates at the first place. If you created a dictionary containing two or more same keys, it will consider the last occurrence irrespective of the value and drop the other(s).

In this case, 'question a': 'pizza' will be dropped and 'question a': 'banana' will be considered as it is the last occurrence here.

  • 1
    The first will be dropped or the last will be dropped? I have never done this since it doesn't work, but the answer above you said the opposite of what you said. – Cfomodz Jul 15 '21 at 19:30
  • @Cfomodz I apologize, the first will be dropped. It will consider the last one. Let me edit it! – NiharParikh17 Jul 15 '21 at 19:31
  • @Cfomodz I just tried in Python 3.8.6 and it keeps "banana". – Mark Ransom Jul 15 '21 at 19:31
  • I figured as much since something like ``` question a = 'pizza' question a = 'banana' ``` Or ``` dict_of_things['thing one'] = pizza dict_of_things['thing one'] = banana Would always result in banana being the value that is kept, but with how assignment can sometimes feel "backward" ``` thing_one = pizza thing_one = thing_one + 'banana' Can seem strange to someone who is new to programming so I honestly wasn't 100% sure how python would handle dict = {'question a':'pizza','question a':'banana'} – Cfomodz Jul 15 '21 at 20:22