Search code examples
pythondictionarykeyerror

Python3 Dictionay KeyError: 264


I've created a dictionary that contains something like following:

{ Location ID : [[latitude, Longitude], District Name]}

eg, { 13: [ [40.703278 , -74.017028], 'Manhattan' ]}

Now i am trying to create and sort out a list of taxi order in another dictionary.

def trip_distribution(list):
    trips = {}
    for x in list:
        zone = zone_list[x[0]][1]
        if zone not in trips:
            trips[zone] = [x]
        else:
            trips[zone] = trips[zone] + [x]
        return trips

But when i ran the codes, it gave me an error that i cannot explain: File "test_function.py", line 144, in trip_distribution zone = zone_list[x[0]][1] KeyError: 264

Anybody knows what is going on?


Solution

  • KeyError: 264 means that it cannot find a key in the dictionary that matches 264.

    it is hard to tell where it is going wrong because we don't know what is in zone_list and list. But try using zone = zone_list[x][1]