You need a spatial structure to speed up lookup for closest zip code. I would approach this problem in two steps: 1. Create a point quad tree and add all known zip codes to it. Latitude and longitude can be as used as coordinates of the tree node and zip code as value. 2. For every zip entry in your myFile, query the quad tree for closest zip code. There are many implementations of quad tree available as Python libraries. You can pick one of them.