## python: how to identify if a variable is an array or a scalar

I have a function that takes the argument `NBins`

. I want to make a call to this function with a scalar `50`

or an array `[0, 10, 20, 30]`

. How can I identify within the function, what the length of `NBins`

is? or said differently, if it is a scalar or a vector?

I tried this:

>>> N=[2,3,5] >>> P = 5 >>> len(N) 3 >>> len(P) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object of type 'int' has no len() >>>

As you see, I can't apply `len`

to `P`

, since it's not an array.... Is there something like `isarray`

or `isscalar`

in python?

thanks

>>> isinstance([0, 10, 20, 30], list) True >>> isinstance(50, list) False

To support any type of sequence, check `collections.Sequence`

instead of `list`

.

**note**: `isinstance`

also supports a tuple of classes, check `type(x) in (..., ...)`

should be avoided and is unnecessary.

You may also wanna check `not isinstance(x, (str, unicode))`

**python: how to identify if a variable is an array or a scalar,** I have a function that takes the argument NBins. I want to make a call to this function with a scalar 50 or an array [0, 10, 20, 30]. How can I identify within the� To identify if a variable is an array or a scalar you can use isinstance method below is the code for the same:->>> isinstance([0, 10, 20, 30], list) True >>> isinstance(50, list) False. To know more about this you can have a look at the following video:-

Previous answers assume that the array is a python standard list. As someone who uses numpy often, I'd recommend a very pythonic test of:

if hasattr(N, "__len__")

**np.isscalar - Numpy and Scipy Documentation,** >>> isinstance([0, 10, 20, 30], list) True >>> isinstance(50, list) False. To support any type of sequence, check collections.Sequence instead of� python: how to identify if a variable is an array or a scalar. I have a function that takes the argument NBins.I want to make a call to this function with a scalar 50 or an array [0, 10, 20, 30].

Combining @jamylak and @jpaddison3's answers together, if you need to be robust against numpy arrays as the input and handle them in the same way as lists, you should use

import numpy as np isinstance(P, (list, tuple, np.ndarray))

This is robust against subclasses of list, tuple and numpy arrays.

And if you want to be robust against all other subclasses of sequence as well (not just list and tuple), use

import collections import numpy as np isinstance(P, (collections.Sequence, np.ndarray))

Why should you do things this way with `isinstance`

and not compare `type(P)`

with a target value? Here is an example, where we make and study the behaviour of `NewList`

, a trivial subclass of list.

>>> class NewList(list): ... isThisAList = '???' ... >>> x = NewList([0,1]) >>> y = list([0,1]) >>> print x [0, 1] >>> print y [0, 1] >>> x==y True >>> type(x) <class '__main__.NewList'> >>> type(x) is list False >>> type(y) is list True >>> type(x).__name__ 'NewList' >>> isinstance(x, list) True

Despite `x`

and `y`

comparing as equal, handling them by `type`

would result in different behaviour. However, since `x`

is an instance of a subclass of `list`

, using `isinstance(x,list)`

gives the desired behaviour and treats `x`

and `y`

in the same manner.

**python: how to identify if a variable is an array or a scalar,** Given an object, the task is to check whether the object is list or not. Method #1: Using isinstance. filter_none. edit close. play_arrow. link brightness_4 code� python: how to identify if a variable is an array or a scalar (7)

Is there an equivalent to isscalar() in numpy? Yes.

>>> np.isscalar(3.1) True >>> np.isscalar([3.1]) False >>> np.isscalar(False) True

**[SOLVED] python: how to identify if a variable is an array or a scalar ,** a = np.array([3, 4, np.inf]) print np.isscalar(a). Output. False. print np.isscalar(len( a)). Output. True. Want to code faster? ⌃. Kite is a plugin for PyCharm, Atom,� Note: Python does not have built-in support for Arrays, but Python Lists can be used instead. Arrays Note: This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a library, like the NumPy library .

While, @jamylak's approach is the better one, here is an alternative approach

>>> N=[2,3,5] >>> P = 5 >>> type(P) in (tuple, list) False >>> type(N) in (tuple, list) True

**Python,** An array might be considered as scalar. The issue is I want to keep a set of values to share among several applications in different languages,� As a signal to other python libraries that this column should be treated as a categorical variable (e.g. to use suitable statistical methods or plot types). Object Creation Categorical object can be created in multiple ways.

**numpy - Determine if a variable is a scalar,** Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal � In order to map function to array (1D and / or 2D) and scalar a_2 * b = array([[2., 4.], [6., 8.]]) I`m using python 2.7 if it is relevant to an issue. python

**check whether a value is scalar - Python,** A Scalar is simply a variable that holds an individual value. An array, vector or matrix cannot be on a scale. Disclaimer: I am unable to find any references of this on the internet, I got the information at school and from old books, amongst� This means that exponent[0] is a scalar, and exponent[0][i] is trying to access it as if it were an array. Did you mean to say: L = identity(len(l)) for i in xrange(len(l)): L[i][i] = exponent[i]

**Determine if any array elements are nonzero,** *args: Arguments (variable number and type) none: in this case, the method only works for arrays with one element (a.size == 1), which element is copied into a standard Python scalar object and returned. int_type: this argument is interpreted as a flat index into the array, specifying which element to copy and return.