3

I found out that some inputs aren't being stored inside my dictionary in Python 3.

Running this code:

N = int(input()) #How many lines of subsequent input
graph = {}

for n in range (N):
    start, end, cost = input().split()

    graph[start] = {end:cost}

    #print("from", start, ":", graph[start])

print(graph)

with input:

3
YYZ SEA 500
YYZ YVR 300
YVR SEA 100

My program outputs:

{'YYZ': {'YVR': '300'}, 'YVR': {'SEA': '100'}}

It seems like the first line mentioning YYZ was overwritten by the second line mentioning YYZ as there is not trace of SEA inside the dictionary.

What is causing this problem and how can I fix it?

Glace
  • 127
  • 1
  • 3
  • 10
  • 5
    That is not a problem; that is how dictionaries work. If you want to save multiple values under 1 key, that value should be a list to which you append new values. – MoxieBall Jul 12 '18 at 19:43
  • 1
    Keys are unique in dictionaries. You are reassigning YYZ to the most recent output. – hancho Jul 12 '18 at 19:45
  • This duplicate is better - [make dictionary with duplicate keys in python](https://stackoverflow.com/q/10664856/2823755) – wwii Jul 13 '18 at 04:43

2 Answers2

5

You are overwriting the value for key 'YYZ' with a replacement value. This is expected behavior for dictionaries. My advice would be to make the values of the dictionary lists rather than single items, so replace your assignment code with something like this

graph.setdefault(start, []).append({end:cost})

Try that and see if that will work with your use case.

BowlingHawk95
  • 1,518
  • 10
  • 15
4

A dictionary can contain only one value per key so that, as expected, the second reference to the key overwrites the first.

What you want to do in this case is store a list of items for each key and append to the list:

start, end, cost = input().split()
if not start in graph:
    graph[start] = []

graph[start].append( {end:cost} )
Larry Lustig
  • 49,320
  • 14
  • 110
  • 160
  • 1
    See my answer for a 1-line alternative that does exactly this. – BowlingHawk95 Jul 12 '18 at 19:46
  • 1
    Seems to me like a prime candidate for a [defaultdict](https://docs.python.org/2/library/collections.html#collections.defaultdict) – Nelewout Jul 12 '18 at 19:49
  • @N.Wouda Yes, you could use a `defaultdict`, but the `.setdefault` method works perfectly well, although I guess some may argue that it's a little less readable. OTOH, a plain `dict` looks nicer when you print it than a `defaultdict` does, but of course that only affects what the developer sees, since a real program doesn't display unformatted data to the user. ;) – PM 2Ring Jul 12 '18 at 20:00