How to Calculate Distance Between Coordinates in Python
Calculating the distance between two points is a fundamental operation in programming, used in everything from game development (collision detection) to logistics (route planning). However, the method you choose depends heavily on the context: are you working on a 2D Cartesian plane (like a screen or map projection) or a spherical surface (like the Earth)?
This guide covers how to calculate Euclidean distance for flat surfaces and Geodesic distance (using the Haversine formula) for geographical coordinates (Latitude/Longitude).
Euclidean Distance (2D/3D Planes)
For flat surfaces (like a game grid) or 3D space, the distance between two points (x_1, y_1) and (x_2, y_2) is calculated using the Pythagorean theorem: d = sqrt((x_2 - x_1)^2 + (y_2 - y_1)^2).
Using the math Module
Python's math module provides straightforward tools for this. While you can write the formula manually, math.hypot (hypotenuse) is cleaner and often faster.
import math
# Point format: (x, y)
p1 = (5, 10)
p2 = (8, 14)
# ⛔️ Manual implementation (Prone to syntax errors/verbose)
dist_manual = math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
print(f"Manual Calc: {dist_manual}")
# ✅ Correct: Using math.dist (Python 3.8+) or math.hypot
# math.dist is the most semantic way to do this
dist_clean = math.dist(p1, p2)
print(f"Clean Calc: {dist_clean}")
Output:
Manual Calc: 5.0
Clean Calc: 5.0
math.dist() is available in Python 3.8+. For older versions, use math.hypot(x2 - x1, y2 - y1).
Geographic Distance (Latitude/Longitude)
You cannot use Euclidean distance for latitude and longitude. Earth is a sphere (oblate spheroid), meaning the distance represented by one degree of longitude shrinks as you move from the Equator to the Poles.
The Haversine Formula
To calculate the "Great Circle" distance (the shortest path over the earth's surface), we use the Haversine formula.
import math
def haversine_distance(lat1, lon1, lat2, lon2):
# Earth radius in kilometers
R = 6371.0
# Convert degrees to radians
lat1_rad, lon1_rad = math.radians(lat1), math.radians(lon1)
lat2_rad, lon2_rad = math.radians(lat2), math.radians(lon2)
# Differences
dlat = lat2_rad - lat1_rad
dlon = lon2_rad - lon1_rad
# Haversine formula
a = math.sin(dlat / 2)**2 + math.cos(lat1_rad) * math.cos(lat2_rad) * math.sin(dlon / 2)**2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
return R * c
# Coordinates for New York and London
ny = (40.7128, -74.0060)
lon = (51.5074, -0.1278)
distance = haversine_distance(ny[0], ny[1], lon[0], lon[1])
print(f"Distance: {distance:.2f} km")
Output:
Distance: 5570.22 km
Using External Libraries (geopy)
For production applications, avoid writing your own Haversine implementation. Edge cases (like the Earth not being a perfect sphere) are handled better by established libraries like geopy.
Installation:
pip install geopy
from geopy.distance import geodesic
# Coordinates: (Latitude, Longitude)
coords_1 = (40.7128, -74.0060) # New York
coords_2 = (51.5074, -0.1278) # London
# ✅ Correct: Calculates distance using the WGS-84 ellipsoid model (more accurate than Haversine)
distance_km = geodesic(coords_1, coords_2).kilometers
distance_miles = geodesic(coords_1, coords_2).miles
print(f"Distance: {distance_km:.2f} km")
print(f"Distance: {distance_miles:.2f} miles")
Output:
Distance: 5585.23 km
Distance: 3470.50 miles
Note the slight difference between the Haversine result (5570 km) and Geopy (5585 km). Geopy is more accurate because it accounts for the flattening of the Earth at the poles.
High Performance with NumPy
If you need to calculate distances between thousands of points (e.g., finding the nearest neighbor in a dataset), Python loops are too slow. Use numpy for vectorized calculations.
import numpy as np
# Define two points in 3D space
p1 = np.array([1, 2, 3])
p2 = np.array([4, 5, 6])
# ✅ Correct: Using Linear Algebra Norm (Euclidean Distance)
dist = np.linalg.norm(p1 - p2)
print(f"NumPy Distance: {dist:.4f}")
Output:
NumPy Distance: 5.1962
Conclusion
- 2D/3D Planes: Use
math.dist(p1, p2)for simplicity. - Geolocation (Quick): Use the Haversine formula if you cannot install external dependencies.
- Geolocation (Accurate): Use
geopy.distance.geodesicfor the most accurate, production-ready results. - Large Datasets: Use
numpy.linalg.normfor performance.