In this tutorial, we will look at how to calculate the Manhattan distance between two points in Python with the help of some exmaples.

## What is Manhattan Distance?

Manhattan distance (also called taxicab distance) is the sum of absolute differences between the coordinates of two points. The following image illustrates the computation of Manhattan distance between points A and B.

You can see that we’re calculating the distance between the two points by moving from point A (1, 1) to B (4, 3) (or vice versa) along the axes at right angles.

### Formula of Manhattan Distance

To calculate the Manhattan distance between the points (x1, y1) and (x2, y2) you can use the formula:

For example, the distance between points (1, 1) and (4, 3) is 5. The above formula can be generalized to n-dimensions:

Highlighted programs for you

Flatiron School

Flatiron School

University of Maryland Global Campus

University of Maryland Global Campus

Creighton University

Creighton University

## Manhattan Distance Computation in Python

There are a number of ways to compute the Manhattan distance in Python. For example, you can write your own custom function or use off the shelf methods from libraries like scipy. Let’s look at them with the help of examples.

### 1. Custom Function in Python

Before we proceed to the off the shelf methods, let’s write a function to get the Manhattan distance between two points with any number of dimensions (assuming both the points have the same number of dimensions).

def get_manhattan_distance(p, q): """ Return the manhattan distance between points p and q assuming both to have the same number of dimensions """ # sum of absolute difference between coordinates distance = 0 for p_i,q_i in zip(p,q): distance += abs(p_i - q_i) return distance # check the function a = (1, 1) b = (4, 3) # distance b/w a and b d = get_manhattan_distance(a, b) # display the result print(d)

Output:

5

You can see that we get the correct result for the points (1,1) and (4,3). The above function also works for points with more than 2 dimensions. For example, let’s get the distance between points (1, 0, 2, 3) and (4, 4, 3, 1).

# check the function a = (1, 0, 2, 3) b = (4, 4, 3, 1) # distance b/w a and b d = get_manhattan_distance(a, b) # display the result print(d)

Output:

10

We get 10 as the output. If we use the formula above, we get the same results.

Note that the above implementation can further be improved by using vectorized operations with the help of numpy arrays to calculate the difference efficiently.

### 2. Manhattan distance using the Scipy Library

The `scipy`

library contains a number of useful functions of scientific computation in Python. Use the `distance.cityblock()`

function available in `scipy.spatial`

to calculate the Manhattan distance between two points in Python.

from scipy.spatial import distance # two points a = (1, 0, 2, 3) b = (4, 4, 3, 1) # mahattan distance b/w a and b d = distance.cityblock(a, b) # display the result print(d)

Output:

10

We get the same results as above. For more on this function, refer to its documentation.

## Difference between Manhattan Distance and Euclidean Distance

When referring to the distance between points we generally are referring to the Euclidean distance between them which is calculated by taking the square root of the sum of squares of differences between the coordinates. On the other hand, in Manhattan distance, we only take the sum of absolute differences between the coordinates of the two points.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**