Find the length of each string element in the Numpy array Last Updated : 02 Jan, 2023 Summarize Comments Improve Suggest changes Share 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 : Numpy Python-numpy Python numpy-arrayManipulation AI-ML-DS With Python Similar Reads Linear Regression in Machine learning Linear regression is a type of supervised machine-learning algorithm that learns from the labelled datasets and maps the data points with most optimized linear functions which can be used for prediction on new datasets. It assumes that there is a linear relationship between the input and output, mea 15+ min read Logistic Regression in Machine Learning Logistic Regression is a supervised machine learning algorithm used for classification problems. Unlike linear regression which predicts continuous values it predicts the probability that an input belongs to a specific class. It is used for binary classification where the output can be one of two po 11 min read K means Clustering â Introduction K-Means Clustering is an Unsupervised Machine Learning algorithm which groups unlabeled dataset into different clusters. It is used to organize data into groups based on their similarity. Understanding K-means ClusteringFor example online store uses K-Means to group customers based on purchase frequ 4 min read K-Nearest Neighbor(KNN) Algorithm K-Nearest Neighbors (KNN) is a supervised machine learning algorithm generally used for classification but can also be used for regression tasks. It works by finding the "k" closest data points (neighbors) to a given input and makesa predictions based on the majority class (for classification) or th 8 min read Introduction to Convolution Neural Network Convolutional Neural Network (CNN) is an advanced version of artificial neural networks (ANNs), primarily designed to extract features from grid-like matrix datasets. This is particularly useful for visual datasets such as images or videos, where data patterns play a crucial role. CNNs are widely us 8 min read Naive Bayes Classifiers Naive Bayes is a classification algorithm that uses probability to predict which category a data point belongs to, assuming that all features are unrelated. This article will give you an overview as well as more advanced use and implementation of Naive Bayes in machine learning. Illustration behind 7 min read Principal Component Analysis(PCA) PCA (Principal Component Analysis) is a dimensionality reduction technique used in data analysis and machine learning. It helps you to reduce the number of features in a dataset while keeping the most important information. It changes your original features into new features these new features donât 7 min read Introduction to Recurrent Neural Networks Recurrent Neural Networks (RNNs) differ from regular neural networks in how they process information. While standard neural networks pass information in one direction i.e from input to output, RNNs feed information back into the network at each step.Lets understand RNN with a example:Imagine reading 10 min read Decision Tree A Decision Tree helps us to make decisions by mapping out different choices and their possible outcomes. Itâs used in machine learning for tasks like classification and prediction. In this article, weâll see more about Decision Trees, their types and other core concepts.A Decision Tree helps us make 6 min read Random Forest Algorithm in Machine Learning Random Forest is a machine learning algorithm that uses many decision trees to make better predictions. Each tree looks at different random parts of the data and their results are combined by voting for classification or averaging for regression. This helps in improving accuracy and reducing errors. 5 min read Like