0

What I need that I can't find a solution for is to add new elements to my dictionary even if there's already that key inside it. So if my dictionary is:

{'red': 1,
 'yellow': 2,
 'green': 3}

and I want to add

'red': 2'

I don't lose the 'red: 1' element, so I have:

{'red': 1,
 'yellow': 2,
 'green': 3,
 'red': 2'}
Riccardo Lamera
  • 105
  • 1
  • 13
  • 2
    This is impossible, dictionaries **must** have **unique** keys. You can use a list of tuples as replacement. Or a dictionary of lists. – mozway Mar 31 '22 at 07:55
  • 3
    Keys in dict are unique, so you can't perform such operation. Instead you can store collection under some key e.q `{'red': [1, 2]}` – kj-crypto Mar 31 '22 at 07:57

0 Answers0