Learning the Manhattan Distance: A Python Tutorial with Examples


Understanding the Manhattan Distance (The City Block Metric)

The concept of measuring distance is absolutely central to fields ranging from mathematics and computer science to advanced data analysis. While most people instinctively think of the shortest path between two points—the Euclidean distance—many practical, real-world constraints necessitate a different metric. The Manhattan distance, often referred to as the L1 norm or the city block distance, offers a vital alternative, particularly when movement is restricted to a grid structure.

Consider the scenario of navigating a typical rectangular street network, much like the famous grid layout of Manhattan, New York. To successfully travel from point A to point B, one cannot simply cut diagonally; movement must strictly follow the horizontal (east-west) and vertical (north-south) street axes. The Manhattan distance precisely quantifies the total distance traveled under these constraints. This characteristic makes it exceptionally useful for applications where the cost of movement is determined by sequential steps along coordinate axes rather than geometric straightness, providing a highly practical measure of dissimilarity.

Unlike the Euclidean metric, which squares the differences between coordinates, the Manhattan distance focuses purely on the absolute differences between the coordinates of two points along each dimension, summing them up directly. This adherence to axial movement ensures that the path taken strictly follows the feature space axes. This simplicity and focus on absolute difference make it a powerful tool for various analytical tasks, including dealing with feature selection or handling high-dimensional data where the calculation of squared distances can become overly sensitive to outliers.

Mathematical Definition and Formal Formula

The mathematical underpinning of the Manhattan distance is straightforward, making it easy to implement across different programming paradigms. The primary mathematical objects involved in this calculation are vectors. Given two points, $A$ and $B$, represented as vectors in an $n$-dimensional space, the Manhattan distance calculates the total difference between them by summing the absolute differences of their corresponding components or coordinates.

Let $A = (A_1, A_2, dots, A_n)$ and $B = (B_1, B_2, dots, B_n)$ represent two vectors residing in $n$-dimensional space. The formal definition for the Manhattan distance, denoted $D_{Manhattan}(A, B)$, is articulated by the following summation formula:

Σ|Ai – Bi|

In this formula, the index $i$ iterates from 1 to $n$, corresponding to the $i^{th}$ element in each vector. Crucially, the absolute value function ensures that the sign or direction of the difference between coordinates is disregarded; only the magnitude of the difference contributes to the final total distance. This simple, linear accumulation of differences is why the Manhattan distance is also known as the L1 norm. This robust definition allows for seamless and straightforward translation into computational code, such as in Python.

This particular distance measure becomes essential when the contribution of differences along each feature dimension should be treated independently and linearly. This is frequently the case in statistical tasks like clustering or classification, especially when feature scales vary significantly or when datasets are inherently sparse, making it a preferred metric over the Euclidean distance in certain contexts.

Key Applications in Data Science and Machine Learning

The selection of an appropriate distance metric holds immense importance, as it directly influences the performance and resulting outcomes of various analytical models. The Manhattan distance finds widespread application across numerous disciplines, particularly within modern machine learning algorithms and complex optimization problems. A significant advantage it holds over the Euclidean distance is its inherent robustness to outliers, which stems from the use of absolute values rather than squared terms, mitigating the disproportionate influence of extreme data points.

One of its most recognizable uses is within the k-Nearest Neighbors (k-NN) classification and regression algorithms. When k-NN employs the L1 norm, it demonstrates improved effectiveness in environments characterized by high-dimensional data. This scenario, often referred to as the curse of dimensionality, tends to diminish the discriminatory power of the Euclidean distance. Furthermore, in specialized tasks such as feature selection, minimizing the sum of absolute errors (which is mathematically equivalent to minimizing the Manhattan distance) frequently results in sparser and more interpretable models.

Beyond traditional classification, the L1 norm provides practical solutions across different domains:

  • Image Processing: It is utilized for precisely measuring differences in color values or tracking positional shifts between pixels in grid-based image representations.
  • Optimization Problems: Essential in areas like linear programming and constraint satisfaction, where movement or resource allocation is strictly confined to orthogonal axes.
  • Data Mining: Used for assessing the degree of dissimilarity between individual records in databases, proving highly effective when dealing with features that are ordinal or categorical after suitable encoding has been applied.

Gaining a comprehensive understanding of when to employ the Manhattan distance in contrast to other metrics, such as the Minkowski or Chebyshev distances, is a foundational skill necessary for developing highly robust and effective machine learning algorithms. The following sections provide practical, step-by-step guidance on implementing this metric efficiently using Python.

Method 1: Implementing a Custom Python Function

For developers requiring maximum control over the underlying calculation logic, or those operating in environments with strict limitations on external library dependencies, defining a custom Python function remains the most direct and reliable approach to calculating the Manhattan distance. This method ensures a direct, faithful translation of the formal mathematical definition into functional, executable Python code, relying only on the language’s built-in capabilities.

The fundamental mechanism requires iterating simultaneously through both input vectors, calculating the absolute difference for every corresponding pair of elements, and aggregating these differences through summation. Python’s native zip() function is ideally suited for this task, as it efficiently pairs elements from two iterables, resulting in remarkably clean and concise implementation code.

The example provided below illustrates the creation of a simple custom function named manhattan. We define two representative sample vectors, $A$ and $B$, and proceed to calculate the resulting distance using this custom function.

from math import sqrt

#create function to calculate Manhattan distance 
def manhattan(a, b):
    return sum(abs(val1-val2) for val1, val2 in zip(a,b))
 
#define vectors
A = [2, 4, 4, 6]
B = [5, 5, 7, 8]

#calculate Manhattan distance between vectors
manhattan(A, B)

9

The final result, 9, is derived from the step-by-step summation: $|2-5| + |4-5| + |4-7| + |6-8|$. This calculation simplifies to $3 + 1 + 3 + 2$, definitively confirming the accuracy and correctness of our manual translation of the mathematical formula into a Python function. While effective for smaller inputs, it is important to note that this technique using standard Python lists and loops is inherently less performant than the highly optimized, vectorized operations offered by specialized numerical libraries when handling extremely large datasets.

Method 2: Leveraging the SciPy Library’s cityblock() for Optimization

Although custom functions are valuable for pedagogy and specific niche cases, large-scale data processing and production environments necessitate leveraging the performance advantages of professionally optimized numerical libraries. The SciPy package, recognized as a fundamental component of scientific computing within the Python ecosystem, provides specialized and highly efficient functions for an extensive range of mathematical operations, including distance calculations.

Specifically, SciPy’s spatial.distance module includes the dedicated function cityblock(), which is engineered precisely for computing the Manhattan distance between two vectors or arrays. Utilizing cityblock() is the recommended standard practice for performance-critical applications, as it relies on underlying optimized routines often written in compiled languages like C or Fortran, delivering substantial speed improvements compared to equivalent implementations written purely in Python.

To utilize this optimized method, the SciPy package must first be correctly installed and imported into the environment. Once imported, the function call is remarkably straightforward, requiring only the two input data structures (vectors or arrays) as positional arguments.

from scipy.spatial.distance import cityblock

#define vectors
A = [2, 4, 4, 6]
B = [5, 5, 7, 8]

#calculate Manhattan distance between vectors
cityblock(A, B)

9

Confirming the consistency across methods, the calculation executed by the optimized cityblock() function returns the identical result of 9. This specialized function not only ensures mathematical accuracy but also represents the professional industry standard for performing robust and high-speed numerical distance calculations within the Python data stack.

Integrating Distance Calculation with Pandas DataFrames

In most realistic data science workflows, data is structured, managed, and manipulated using the highly versatile Pandas DataFrame object. A significant advantage of SciPy’s implementation is the flexibility of the cityblock() function, which seamlessly handles data stored within these columnar structures. This allows data scientists to easily calculate the distance between columns (representing features) or rows (representing observations).

When we compute the distance between two columns of a DataFrame, we are effectively treating each column as a distinct vector in the feature space. This capability is exceptionally valuable for comparative analysis, such as measuring the intrinsic similarity or dissimilarity between the distributions of two different variables within the dataset. The following code demonstrates the process of initializing a DataFrame and subsequently calculating the Manhattan distance between the column labeled ‘A’ and the column labeled ‘B’.

It is important to recognize that when accessing specific columns within a DataFrame, Pandas extracts them as Series objects. The SciPy distance functions are engineered to process these Series objects with high efficiency, treating them internally as numerical arrays, thus maintaining performance even when working with complex structured data.

from scipy.spatial.distance import cityblock
import pandas as pd

#define DataFrame
df = pd.DataFrame({'A': [2, 4, 4, 6],
                   'B': [5, 5, 7, 8],
                   'C': [9, 12, 12, 13]})

#calculate Manhattan distance between columns A and B 
cityblock(df.A, df.B)

9

This example powerfully showcases the seamless and efficient integration of dedicated numerical libraries within the broader Python data ecosystem. Regardless of whether the starting data format is a simple list or a sophisticated Pandas DataFrame, calculating the Manhattan distance remains a fast and reliable operation when utilizing the optimized SciPy implementation.

Summary and Further Exploration

The Manhattan distance, or L1 norm, stands as a critically important metric in quantitative data analysis, particularly effective when modeling constrained movement along orthogonal axes or when increased resistance to data outliers is necessary. Throughout this guide, we thoroughly explored two distinct methodologies for its calculation in Python: the development of a functional implementation from first principles, and the utilization of the highly optimized cityblock() function provided by the SciPy package.

For the vast majority of production-level machine learning and data science tasks, relying on the SciPy library is unequivocally the recommended standard practice due to its substantial efficiency gains, superior numerical stability, and robust handling of various data structures, including NumPy arrays and Pandas Series. Nevertheless, understanding the custom implementation serves to reinforce the core mathematical principles that underpin the calculation, providing a deeper conceptual understanding.

To continue building expertise in distance metrics and their profound influence on analytical models, we recommend exploring the following advanced topics:

  • A detailed, comparative analysis of the trade-offs between the Euclidean distance and the Manhattan distance.
  • Investigating precisely how various distance metrics fundamentally influence the convergence and performance of common clustering algorithms, such as K-Means.
  • A practical implementation guide for the Minkowski distance, which serves as a powerful generalization encompassing both the Euclidean and Manhattan metrics.

Achieving mastery over these foundational distance measures is an indispensable prerequisite for any professional engaging with quantitative data, advanced statistical modeling, or contemporary machine learning algorithms.

Cite this article

Mohammed looti (2025). Learning the Manhattan Distance: A Python Tutorial with Examples. PSYCHOLOGICAL STATISTICS. Retrieved from https://statistics.arabpsychology.com/calculate-manhattan-distance-in-python-with-examples/

Mohammed looti. "Learning the Manhattan Distance: A Python Tutorial with Examples." PSYCHOLOGICAL STATISTICS, 4 Nov. 2025, https://statistics.arabpsychology.com/calculate-manhattan-distance-in-python-with-examples/.

Mohammed looti. "Learning the Manhattan Distance: A Python Tutorial with Examples." PSYCHOLOGICAL STATISTICS, 2025. https://statistics.arabpsychology.com/calculate-manhattan-distance-in-python-with-examples/.

Mohammed looti (2025) 'Learning the Manhattan Distance: A Python Tutorial with Examples', PSYCHOLOGICAL STATISTICS. Available at: https://statistics.arabpsychology.com/calculate-manhattan-distance-in-python-with-examples/.

[1] Mohammed looti, "Learning the Manhattan Distance: A Python Tutorial with Examples," PSYCHOLOGICAL STATISTICS, vol. X, no. Y, ص Z-Z, November, 2025.

Mohammed looti. Learning the Manhattan Distance: A Python Tutorial with Examples. PSYCHOLOGICAL STATISTICS. 2025;vol(issue):pages.

Download Post (.PDF)
Scroll to Top