  # Find length of each string element in Numpy array

In this post, we are going to discuss the ways we can find the length of each string element in a given numpy array.

Problem # 1: Given an empty array whose underlying data has string type. Find the length of each element in a given object.

Solution: we're going to use ` numpy.vectorize () ` to find the length of each element in a given numpy array object.

` `

` # import numpy library as np import numpy as np   # Create an empty array arr = np.array ([ 'New York' , ' Lisbon' , 'Beijing' , ' Quebec' ])   # Print array print (arr) `

` `

Output: We will now use ` numpy.vectorize () ` to find the length of each item in a given array object numpy.

 ` # Use numpy vectorization ` ` 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 computed the length of each string element in the given numpy array object.

Problem # 2: An array is given with an empty array, the underlying data of which is of a string type. Find the length of each element in a given object.

Solution: We're going to use a list comprehension technique to find the length of each element in a given array object.

 ` # import numpy library as np ` ` import ` ` numpy as np `   ` # Create an empty array ` ` arr ` ` = ` ` np.array ([` `' New York' ` `, ` ` 'Lisbon' ` `, ` ` 'Beijing' ` `, ` `' Quebec' ` `]) ` ` `  ` # Print array ` ` print ` ` (arr) `

Output: We will now use a list comprehension technique to find the length of each element in a given numpy array object.

 ` # Find the length of each element ` ` arr_len ` ` = ` ` [` ` len ` ` (i) ` ` for ` ` i ` ` in ` ` arr] `   ` # Print the length of each element ` ` pr int ` ` (arr_len) `

Output: Like us we see in the output, we have successfully computed the length of each string element in the given numpy array object.