Find the length of each string element in the Numpy array Last Updated : 02 Jan, 2023 Comments Improve Suggest changes Like Article Like Report NumPy builds on (and is a successor to) the successful Numeric array object. Its goal is to create the corner-stone for a useful environment for scientific computing. NumPy provides two fundamental objects: an N-dimensional array object (ndarray) and a universal function object (ufunc). In this post we are going to discuss ways in which we can find the length of each string element in the given numpy array. Problem #1 : Given a numpy array whose underlying data is of string type. Find the length of each element in the given object. Solution : We are going to use numpy.vectorize() function to find the length of each element in the given numpy array object. Python3 # importing the numpy library as np import numpy as np # Create a numpy array arr = np.array(['New York', 'Lisbon', 'Beijing', 'Quebec']) # Print the array print(arr) Output : Now we will use numpy.vectorize() function to find the length of each element in the given numpy array object. Python3 # Use vectorize function of numpy length_checker = np.vectorize(len) # Find the length of each element arr_len = length_checker(arr) # Print the length of each element print(arr_len) Output : As we can see in the output, we have successfully calculated the length of each string element in the given numpy array object. Problem #2 : Given a numpy array whose underlying data is of string type. Find the length of each element in the given object. Solution : We are going to use List comprehension technique to find the length of each element in the given numpy array object. Python3 # importing the numpy library as np import numpy as np # Create a numpy array arr = np.array(['New York', 'Lisbon', 'Beijing', 'Quebec']) # Print the array print(arr) Output : Now we will use List comprehension technique to find the length of each element in the given numpy array object. Python3 # Find the length of each element arr_len = [len(i) for i in arr] # Print the length of each element print(arr_len) Output : As we can see in the output, we have successfully calculated the length of each string element in the given numpy array object. One additional approach to finding the length of each element in a NumPy array is to use the NumPy function numpy.char.str_len. This function takes a NumPy array of strings as input and returns an array of the lengths of each string. For example: Python3 import numpy as np # Create a numpy array arr = np.array(['New York', 'Lisbon', 'Beijing', 'Quebec']) # Find the length of each element arr_len = np.char.str_len(arr) # Print the length of each element print(arr_len) #This code is contributed by Edula Vinay Kumar Reddy This will output: [8 7 7 6] (This code wont work in gfg compiler , install numpy in your local compiler then check ) Comment More infoAdvertise with us Next Article Find the length of each string element in the Numpy array S Shubham__Ranjan Follow Improve Article Tags : Python Numpy Python-numpy Python numpy-arrayManipulation AI-ML-DS With Python +1 More Practice Tags : python Similar Reads How to check whether the elements of a given NumPy array is non-zero? In NumPy with the help of any() function, we can check whether any of the elements of a given array in NumPy is non-zero. We will pass an array in the any() function if it returns true then any of the element of the array is non zero if it returns false then all the elements of the array are zero. S 1 min read Find length of one array element in bytes and total bytes consumed by the elements in Numpy In NumPy we can find the length of one array element in a byte with the help of itemsize . It will return the length of the array in integer. And in the numpy for calculating total bytes consumed by the elements with the help of nbytes. Syntax: array.itemsize  Return :It will return length(int) of 1 min read Test whether the elements of a given NumPy array is zero or not in Python In numpy, we can check that whether none of the elements of given array is zero or not with the help of numpy.all() function. In this function pass an array as parameter. If any of one element of the passed array is zero then it returns False otherwise it returns True boolean value. Syntax: numpy.al 2 min read How to split the element of a given NumPy array with spaces? To split the elements of a given array with spaces we will use numpy.char.split(). It is a function for doing string operations in NumPy. It returns a list of the words in the string, using sep as the delimiter string for each element in arr. Parameters:arr : array_like of str or unicode.Input array 2 min read Find indices of elements equal to zero in a NumPy array Sometimes we need to find out the indices of all null elements in the array. Numpy provides many functions to compute indices of all null elements. Method 1: Finding indices of null elements using numpy.where() This function returns the indices of elements in an input array where the given conditio 3 min read NumPy| How to get the unique elements of an Array To find unique elements of an array we use the numpy.unique() method of the NumPy library in Python. It returns unique elements in a new sorted array. Example: Python3 import numpy as np arr = np.array([1, 2, 3, 1, 4, 5, 2, 5]) unique_elements = np.unique(arr) print(unique_elements) Output: [1 2 3 4 2 min read Find the memory size of a NumPy array In this post, we will see how to find the memory size of a NumPy array. So for finding the memory size of a NumPy array we are using following methods: Using size and itemsize attributes of NumPy array size: This attribute gives the number of elements present in the NumPy array. itemsize: This attri 2 min read How to find the Index of value in Numpy Array ? In this article, we are going to find the index of the elements present in a Numpy array.Using where() Methodwhere() method is used to specify the index of a particular element specified in the condition.Syntax: numpy.where(condition[, x, y])Example 1: Get index positions of a given valueHere, we fi 5 min read NumPy - Arithmetic operations with array containing string elements Numpy is a library of Python for array processing written in C and Python. Computations in numpy are much faster than that of traditional data structures in Python like lists, tuples, dictionaries etc. due to vectorized universal functions. Sometimes while dealing with data, we need to perform arith 2 min read NumPy ndarray.size() Method | Get Number of Elements in NumPy Array The ndarray.size() method returns the number of elements in the NumPy array. It works the same as np.prod(a.shape), i.e., the product of the dimensions of the array. Example Python3 import numpy as np arr = np.zeros((3, 4, 2), dtype = np.complex128) gfg = arr.size print (gfg) Output : 24Syntax Synta 1 min read Like