Join thousands of students in our LangChain and Vector DBs in Production course, with over 50+ lessons and practical projects for FREE!.

Programming

# Find the Length of an Array in Python

Last Updated on March 22, 2022 by Editorial Team

#### Author(s): Pratik Shukla

Originally published on Towards AI the World’s Leading AI and Technology News and Media Company. If you are building an AI-related product or service, we invite you to consider becoming an AI sponsor. At Towards AI, we help scale AI and technology startups. Let us help you unleash your technology to the masses.

#### Python Programming 101 Tutorials — A Pure Logical Approach

In this series of articles, we will implement some basic practice problems in Python. Our ultimate goal is to improve logical thinking. So, we will not be using any Python libraries in the implementation. Please let us know if you have any suggestions or comments.

Let’s get started!

#### Practice Problem #1:

You are given an array arr of n elements. Your goal is to find the length n of the array without using any inbuilt functions like len(). Take the following input and output as an example.
Input = [1,2,3,4,5]
Output = 5

Now, take some time to understand the problem and implement it in Python.

#### An Explanation for the Solution of Practice Problem #1:

• First of all, we are creating a function find_len(arr). The function takes an array arr as an argument. Our goal is to find the length of the array arr.
• Next, we define a variable count and set its value to 0. In the end, we will return the updated value of countas the length of the input array. We are initializing it with 0 because, at the moment, we don’t know the size of the input array.
• Next, we are running a for loop to go through all the elements of the array. Please note that the for loop will terminate only after iterating through all the elements of the input array.
• As we iterate through the elements of the array, we will increment the value of count by 1 to keep track of the number of elements in the array.
• Once the for loop terminates, we return the value of count. The count variable here gives us the length of the input array arr.
• In the next step, we are just calling the function and printing the value.

Let’s have a look at how to operation is performed with some graphics.

? Check out our editorial recommendations for the best deep learning workstations. ?

The green nodes represent the current position of the iteration. The nodes colored in red represent visited nodes.

• Initially, no nodes are visited, and we have not entered the for loop.
• The 1st iteration of the for loop.
• The 2nd iteration of the for loop.
• The 3rd iteration of the for loop.
• The 4th iteration of the for loop.
• The 5th iteration of the for loop.
• Since there are no more elements to iterate, our for loop will terminate and return the current value of count.

So, this is how we can find the length of an array without using any inbuilt functions in Python. We hope you guys learned something new from it. If you have any suggestions or comments, please feel free to leave a comment. Thanks for reading!

#### Resources:

Find the Length of an Array in Python was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Join thousands of data leaders on the AI newsletter. It’s free, we don’t spam, and we never share your email address. Keep up to date with the latest work in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI