0

i have a list of tuples:

points = [(1, 2), (1, 5), (2, 5), (3, 3), (3, 7), (6, 5), (9, 9)]

when i try to convert it to a dictionary:

dictionary = dict(points)

the result i get is:

{1: 5, 2: 5, 3: 7, 6: 5, 9: 9}

its removing the 1:5 and 3:3 points.

How do i get to have all the points? the desired outcome is :

{1: 2, 1: 5, 2: 5, 3: 3, 3: 7, 6: 5, 9: 9}

thanks in advance

  • 3
    I don't think python support dictionaries with the same name keys... https://stackoverflow.com/questions/10664856/make-a-dictionary-with-duplicate-keys-in-python – Sachin Kohli Oct 02 '22 at 10:12
  • What do you expect `dictionary[1]` to return? – Mechanic Pig Oct 02 '22 at 10:16
  • It's removing the `1:2` pair, not the `1:5` pair. But it's not really removing it, it's overwriting it with `1:5`. The whole point of a dictionary (aka hash table) is that each key has a single value associated with it. That value may be an aggregate type, but the structure you suggest wouldn't make sense for a dictionary. The best you could do is have a list, or tuple, or set associated with each key, e.g. `{1 : (2, 5), ...}` Just decide what your needs are, then reason it out. – Tom Karzes Oct 02 '22 at 10:20

0 Answers0