# Only length-1 arrays can be converted to python scalars

Numpy throws error – only length-1 (size-1) arrays can be converted to python scalars when you are passing an array to a Python inbuild function which expects single value. This could happen with `int()`, `float()`, `bytes()` etc. Get whole list of scalar types here.

Note: Some articles suggest that it’s a `matplotlib` error but it is not. Neither it has anything to do with it. It’s a Python error raised by passing wrong arguments to a function.

## Problem

Problem is that you are passing an array of length > 1 to a function like `int()`, which expect single digit. So, this will generate the error –

``````arr = [1, 2, 3.4, 5.6, "8"]

print(int(arr))

Error: only length-1 arrays can be converted to python scalars``````

## Solution

Simple, don’t pass an array. Pass a single value. Or, you may pass a single value array. i.e. an array of length 1.

First of all we need to understand our use case. Why we are passing an array to `int()` or `float()`? Most probably you want to convert the type of each value of array. For example, converting all string or float values in array to int. Like this –

``````arr = [1, 2, 3.4, 5.6, "8"]

// convert to [1, 2, 3, 5, 8]``````

If you pass this `arr` to `int()` function, you will get only length-1 (size-1) arrays can be converted to python scalars error.

So how to convert it? There are two solutions –

1. Loop over each value of array and convert into `int()`.
2. Use `vectorize()` function of numpy.

### Solution 1: Using Loop

``````arr = [1, 2, 3.4, 5.6, "8"]

for index in range(len(arr)):
arr[index] = int(arr[index])

print(arr)

# Output: [1, 2, 3, 5, 8]``````

Here I am using a for loop to access individual values of array. Then we are type casting each value to int and store them back to the array.

### Solution 2: Using numpy `vectorize()`

If you are using numpy then you can use it’s `vectorize()` function. Internally it runs a loop over each value of the array and do computations according to provided function. So, it accepts array and a custom function.

``````import numpy as np

arr = [1, 2, 3.4, 5.6, "8"]

def myfunc(a):
return int(a)

vectorfunc = np.vectorize(myfunc)

print(vectorfunc(arr))

# Output: [1, 2, 3, 5, 8]``````

## Conclusion

Sometimes lenient languages like Python and Php could be strict too. Especially when you are breaking the rules. In this case, we were doing the same. Passing an array to int() which could only handle single value. Resulting in only length-1 (size-1) arrays can be converted to python scalars error. But we saw that we can very easily solve this by running loop over each item or by using numpy vectorize function.

## Live Demo

Demo might not work in private window (incognito)

Get only the best articles delivered to your mail

## ‘nonetype’ object has no attribute ‘append’ – Python Error

Python throws error ‘nonetype’ object has no attribute ‘append’ when we try to store the outcome of append in the array. Let’s understand this with an example – This code…

## Python TypeError: can’t multiply sequence by non-int of type ‘float’

Python throws typeerror: can’t multiply sequence by non-int of type ‘float’ when you try to multiply a string with float value. Although you can multiply a string with int value…

## Python TypeError: Unhashable Type: ‘list’

Table of Contents Hide IntroductionCode ExampleHow do I fix TypeError Unhashable type list in Python?What does unhashable mean in Python?What is hash() in Python?Live DemoRelated Posts: Python throws typeerror: unhashable… ## python run bash script in background

Table of Contents Hide IntroductionTerminating background script after a timeoutHow to check if process is still running or terminated?How to get output from process?ConclusionLive DemoRelated Posts: Python subprocess library is…

## ‘tuple’ object is not callable – Python Error

Python throws the error, ‘tuple’ object is not callable, when you forget to separate members using comma in single or multidimensional tuples. Consider this example – This code will throw…

## ‘series’ objects are mutable, thus they cannot be hashed – Python Error

Python throws the error, ‘series’ objects are mutable, thus they cannot be hashed when a mutable object like array is used as dictionary key for some other object. According to…