How to find the distance between two points in Python

Learn how to find the distance between two points in Python. Discover different methods, real-world applications, and common error fixes.

How to find the distance between two points in Python
Published on: 
Wed
Mar 25, 2026
Updated on: 
Thu
Mar 26, 2026
The Replit Team

Calculate the distance between two points in Python. This is a common task in fields like data science and game development. Python's math module offers simple tools for this calculation.

In this article, you'll learn several techniques to find this distance. We'll cover practical tips, real-world applications, and common debugging advice to help you implement these methods in your projects.

Basic Euclidean distance formula

def distance(point1, point2):
return ((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2)**0.5

point1 = (3, 4)
point2 = (6, 8)
print(f"Distance between {point1} and {point2}: {distance(point1, point2)}")--OUTPUT--Distance between (3, 4) and (6, 8): 5.0

The distance function translates the Euclidean distance formula directly into Python. It calculates the straight-line path between two points on a plane. The function's core uses the **0.5 operator, an efficient way to find the square root without importing the math module. This approach keeps the function independent and lightweight.

The calculation breaks down into a few key steps:

  • It finds the difference between the x and y coordinates.
  • Each difference is squared using the **2 operator.
  • Finally, it adds the squared results and takes the square root of their sum.

Leveraging Python libraries for distance calculations

While the manual approach works well for simple cases, Python’s specialized libraries offer more robust and efficient tools for the job.

Using the math module

import math

def distance_with_math(point1, point2):
return math.sqrt((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2)

point1 = (3, 4)
point2 = (6, 8)
print(f"Distance: {distance_with_math(point1, point2)}")--OUTPUT--Distance: 5.0

The math module offers a more explicit way to handle these calculations. This function, distance_with_math, replaces the **0.5 operator with math.sqrt(). While both methods produce the same outcome, using the dedicated function improves readability.

  • It makes your code's intention clearer—you're explicitly calculating a square root.
  • This approach is often preferred in professional codebases for its clarity and directness.

Using numpy for vector-based calculations

import numpy as np

point1 = np.array([3, 4])
point2 = np.array([6, 8])
distance = np.linalg.norm(point1 - point2)
print(f"Distance using NumPy: {distance}")--OUTPUT--Distance using NumPy: 5.0

For more complex tasks, especially in data science, numpy is the go-to library. It treats points as vectors using np.array, which unlocks highly efficient, vectorized operations. This approach is much faster than looping through coordinates when you're working with large amounts of data.

  • The calculation is concise. It first subtracts the two numpy arrays to get a vector representing the difference between the points.
  • Then, np.linalg.norm() computes the vector's magnitude—its length—which is the Euclidean distance.

Calculating with scipy.spatial.distance

from scipy.spatial import distance as dist

point1 = (3, 4)
point2 = (6, 8)
distance = dist.euclidean(point1, point2)
print(f"Euclidean distance: {distance}")--OUTPUT--Euclidean distance: 5.0

The scipy library offers a specialized toolkit for scientific and technical computing. Its scipy.spatial.distance module provides a collection of functions for calculating distances, making your code clean and explicit.

  • The dist.euclidean() function directly computes the distance, so you don't need to write the formula yourself.
  • This is especially useful when your work involves various distance metrics, as scipy includes many others beyond Euclidean.

Advanced distance calculation techniques

These libraries scale elegantly to handle more complex scenarios, from working in higher dimensions to calculating distances between many points using various metrics.

Working in higher dimensions

import numpy as np

# Distance in 3D space
point1_3d = np.array([1, 2, 3])
point2_3d = np.array([4, 5, 6])
distance_3d = np.linalg.norm(point1_3d - point2_3d)
print(f"3D distance: {distance_3d}")--OUTPUT--3D distance: 5.196152422706632

Using numpy scales beautifully to higher dimensions. The same np.linalg.norm() function you used for 2D points works perfectly for 3D coordinates or even more complex spaces. You just need to provide arrays with more values.

  • The code defines two 3D points using np.array.
  • The distance calculation itself doesn't change, which highlights the power of vectorized operations. This method works for any number of dimensions without you needing to rewrite the underlying formula.

Calculating distances between multiple points

import numpy as np

# Calculate distances between multiple points at once
points_a = np.array([[1, 2], [3, 4], [5, 6]])
points_b = np.array([[7, 8], [9, 10], [11, 12]])
distances = np.linalg.norm(points_a - points_b, axis=1)
print(f"Batch distances: {distances}")--OUTPUT--Batch distances: [8.48528137 8.48528137 8.48528137]

numpy excels at batch operations, making it easy to calculate distances between multiple pairs of points simultaneously. The code subtracts the points_b array from points_a, a vectorized operation that's much faster than a traditional loop.

  • The key is the axis=1 argument in the np.linalg.norm() function.
  • This parameter instructs numpy to compute the norm for each row, treating each as a separate vector.
  • As a result, you get an array of distances corresponding to each pair of points, all calculated in one go.

Using alternative distance metrics with scipy

from scipy.spatial import distance

point1 = (3, 4)
point2 = (6, 8)
manhattan_dist = distance.cityblock(point1, point2)
chebyshev_dist = distance.chebyshev(point1, point2)
print(f"Manhattan distance: {manhattan_dist}")
print(f"Chebyshev distance: {chebyshev_dist}")--OUTPUT--Manhattan distance: 7.0
Chebyshev distance: 4.0

Beyond Euclidean distance, scipy provides a variety of other metrics. The distance.cityblock() function, for example, calculates the Manhattan distance—think of it as the path you'd take moving along a city grid. It simply sums the absolute differences of the coordinates.

  • The distance.chebyshev() function computes the Chebyshev distance, which is the greatest difference along any single coordinate dimension.
  • This flexibility makes scipy a powerful tool when straight-line paths don't fit your problem, such as in logistics or chessboard-based games.

Move faster with Replit

Replit is an AI-powered development platform that transforms natural language into working applications. Describe what you want to build, and Replit Agent creates it—complete with databases, APIs, and deployment.

For the distance calculation techniques we've explored, Replit Agent can turn them into production-ready tools. You could build:

  • A logistics tool that calculates the shortest path between multiple warehouse and delivery locations using numpy's batch processing.
  • A proximity alert system for a game that notifies players when they are near an objective, leveraging scipy.spatial.distance.
  • A data visualization dashboard that maps the Euclidean or Manhattan distance between geographic coordinates.

Describe your app idea, and Replit Agent writes the code, tests it, and fixes issues automatically, all in your browser. Try Replit Agent and bring your concept to life.

Common errors and challenges

When calculating distances, you might run into a few common issues, from incorrect data types to mismatched dimensions.

Handling type errors with point coordinates

A frequent problem is the TypeError, which often happens when your point coordinates are stored as strings instead of numbers. Mathematical operations like subtraction or squaring can't work on text, so an expression like '6' - '3' will fail.

  • Always ensure your coordinates are integers or floats before passing them to a distance function. You can use int() or float() to convert them if needed.
  • This simple check prevents your calculations from breaking unexpectedly due to incorrect data formats.

Fixing dimension mismatch errors in numpy arrays

When using libraries like numpy, you may encounter errors if your arrays don't have the same dimensions. For instance, trying to calculate the distance between a 2D point and a 3D point will cause a mismatch because the arrays have different lengths.

  • Before performing vector operations, verify that your numpy arrays have the same shape.
  • This ensures that operations like subtraction work element-wise as intended, preventing errors and giving you accurate results.

Avoiding index errors when accessing point coordinates

An IndexError occurs when you try to access a coordinate that doesn't exist, such as requesting a z-coordinate like point[2] from a 2D point tuple like (3, 4). This is common in manual implementations where you assume a certain data structure.

  • Double-check the length or structure of your points before accessing their coordinates by index.
  • Validating that a point is 2D, 3D, or another dimension helps you avoid trying to access elements that aren't there.

Handling type errors with point coordinates

This error can be tricky because a point tuple might look correct at first glance. But if even one coordinate is a string, mathematical operations will fail. The code below shows what happens when a point contains mixed data types, triggering a TypeError.

def distance(point1, point2):
return ((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2)**0.5

point1 = (3, "4") # Second coordinate is a string
point2 = (6, 8)
print(f"Distance between {point1} and {point2}: {distance(point1, point2)}")

The calculation fails at point1[1] - point2[1], where Python cannot subtract the integer 8 from the string "4". This data type mismatch raises the TypeError. The corrected implementation is shown next.

def distance(point1, point2):
x1, y1 = float(point1[0]), float(point1[1])
x2, y2 = float(point2[0]), float(point2[1])
return ((x1 - x2)**2 + (y1 - y2)**2)**0.5

point1 = (3, "4") # Second coordinate is a string
point2 = (6, 8)
print(f"Distance between {point1} and {point2}: {distance(point1, point2)}")

The corrected function proactively converts each coordinate to a number using float(). This makes the code more resilient, as it can now handle coordinates that are accidentally stored as strings. You'll often encounter this issue when processing data from external sources, like CSV files or user input, where numbers are frequently read as text. By converting upfront, you prevent a TypeError from crashing your program.

Fixing dimension mismatch errors in numpy arrays

When using numpy, your arrays must have matching shapes for operations like subtraction to work. A dimension mismatch—like comparing a 2D point to a 3D point—will cause a ValueError because the operation can't be performed element-wise. The code below demonstrates this issue.

import numpy as np

point1 = np.array([3, 4])
point2 = np.array([6, 8, 2]) # Extra dimension
distance = np.linalg.norm(point1 - point2) # Will raise ValueError
print(f"Distance using NumPy: {distance}")

The subtraction point1 - point2 fails because the arrays have different lengths, which prevents numpy from performing the element-wise operation. The corrected implementation below shows how to handle this scenario.

import numpy as np

point1 = np.array([3, 4])
point2 = np.array([6, 8, 2]) # Extra dimension

min_dims = min(point1.shape[0], point2.shape[0])
distance = np.linalg.norm(point1[:min_dims] - point2[:min_dims])
print(f"Distance using NumPy (first {min_dims} dimensions): {distance}")

The corrected code resolves the ValueError by calculating the distance using only the common dimensions between the two points. It finds the minimum number of dimensions with min() and then slices both arrays to that length. This ensures the arrays have matching shapes before subtraction, preventing the error. Keep an eye out for this issue when working with datasets where points might have inconsistent dimensions, as it's a common source of bugs.

Avoiding index errors when accessing point coordinates

An IndexError often occurs when a point is missing coordinates that your function expects. For example, trying to access the y-coordinate with point[1] on a tuple that only contains an x-value will cause your program to crash. The following code demonstrates this issue.

def distance(point1, point2):
return ((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2)**0.5

point1 = (3,) # Missing second coordinate
point2 = (6, 8)
print(f"Distance between {point1} and {point2}: {distance(point1, point2)}")

The code fails because point1 is a single-element tuple. The function's attempt to access the second coordinate with point1[1] results in an IndexError since that index is out of bounds. See how to fix this below.

def distance(point1, point2):
if len(point1) < 2 or len(point2) < 2:
raise ValueError("Points must have at least 2 coordinates")
return ((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2)**0.5

try:
point1 = (3,) # Missing second coordinate
point2 = (6, 8)
print(f"Distance between {point1} and {point2}: {distance(point1, point2)}")
except ValueError as e:
print(f"Error: {e}")

The corrected function prevents the IndexError by first checking the length of each point tuple with len(). If either point has fewer than two coordinates, it raises a ValueError with a helpful message. This validation step makes your function more robust.

It's a good practice when your code processes data from files or user input, where incomplete entries are common. This approach provides clearer feedback than a generic IndexError and stops the program from crashing unexpectedly.

Real-world applications

These distance calculations move beyond abstract formulas to become practical tools for solving real-world problems in data science.

Finding the nearest neighbor with numpy

One of the most practical applications of distance calculation is finding the “nearest neighbor” in a dataset, which numpy accomplishes efficiently with vectorized operations.

import numpy as np

# Sample data points (e.g., locations on a map)
points = np.array([[1, 2], [3, 5], [8, 1], [5, 4], [7, 6]])
query_point = np.array([4, 3]) # Our current location

distances = np.linalg.norm(points - query_point, axis=1)
nearest_idx = np.argmin(distances)
print(f"Nearest point to {query_point} is {points[nearest_idx]}")
print(f"Distance: {distances[nearest_idx]:.2f}")

This code efficiently finds which point in a list is closest to a target query_point. It’s a great example of numpy's power for batch operations, as the calculation happens in a few key steps:

  • First, numpy uses broadcasting to subtract the query_point from every point in the main array simultaneously.
  • Next, np.linalg.norm computes the distance for each resulting vector along axis=1.
  • Finally, np.argmin returns the index of the minimum distance, which pinpoints the closest point in the original list.

Computing text document similarity with vector distances

You can apply the same distance calculations to measure how similar text documents are by first representing them as numerical vectors of word frequencies.

import numpy as np

# Simple word frequency vectors for two documents
# Each vector counts occurrences of words: [the, is, python, code, distance]
doc1 = np.array([2, 1, 3, 1, 2]) # First document word frequencies
doc2 = np.array([1, 2, 2, 3, 1]) # Second document word frequencies

# Calculate document similarity using Euclidean distance
similarity = np.linalg.norm(doc1 - doc2)
print(f"Document vectors:\n Doc1: {doc1}\n Doc2: {doc2}")
print(f"Euclidean distance between documents: {similarity:.2f}")

This technique turns text comparison into a geometric problem. The code represents two documents as numerical vectors—doc1 and doc2—where each number is a word's frequency count. This process is a foundational step in natural language processing.

  • The calculation first finds the difference between the two vectors.
  • Then, np.linalg.norm() computes the Euclidean distance of that resulting vector.

A smaller distance indicates the documents are more similar because their word usage patterns are closer.

Get started with Replit

Turn these concepts into a real tool with Replit Agent. Describe what you want, like “a tool to find the nearest delivery point using numpy” or “a dashboard that maps Manhattan distances between coordinates.”

Replit Agent writes the code, tests for errors, and deploys your app right from your browser. Start building with Replit.

Get started free

Create and deploy websites, automations, internal tools, data pipelines and more in any programming language without setup, downloads or extra tools. All in a single cloud workspace with AI built in.

Get started for free

Create & deploy websites, automations, internal tools, data pipelines and more in any programming language without setup, downloads or extra tools. All in a single cloud workspace with AI built in.