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.
A solution to Practice ProblemΒ #1:
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:
- You can access the code files on Google Colab andΒ GitHub.
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