How to Find the Number of Elements in a Python Array?

Have you ever come across a situation where you must find the number of elements in a Python array? In one of my projects for USA clients, I needed to determine the number of items in an array, In this article, I explained different ways of finding the number of elements in a Python array, along with practical examples and screenshots of executed codes.

What is an Array in Python?

Before looking at how to find the number of elements in a Python array, let us understand what an array is in Python. An array is a collection of items stored at contiguous memory locations. It allows you to store multiple items of the same datatype.

Check out How to Sort an Array in Python

Use the len() Function

Python built-in len() function is a frequently used method to find the number of elements in a Python array. The len() function returns the length of an object, Here it is the number of elements in the array.

Let us understand this with the help of an example:

students = ["John", "Emily", "Michael", "Emma", "William"]
num_students = len(students)
print("Number of students:", num_students)

Output:

Number of students: 5

Refer to the screenshot below of the executed example code for a better understanding.

Number of Elements in a Python Array

In the above example, we have an array called students that contains the names of five students. By passing the students array to the len() function, we can get the number of elements in the array, which is 5.

Read How to Use Python Array Index -1

Example

Let me tell you a real world scenario. Suppose you are a teacher in a school in New York, and you want to keep track of the number of students in your class. You can store the student names in an array and use the len() function to determine the class size.

class_students = ["Olivia", "Liam", "Ava", "Noah", "Isabella", "Ethan"]
class_size = len(class_students)
print("Number of students in the class:", class_size)

Output:

Number of students in the class: 6

Let us understand how the above example works in the screenshot given below.

Find the Number of Elements in a Python Array

Check out How to Convert an Array to a Tuple in Python

Iterating and Counting Elements

Another approach to finding the number of elements in a Python array is by iterating over the array and keeping a count of the elements.

Consider the example below:

cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
count = 0
for city in cities:
    count += 1
print("Number of cities:", count)

Output:

Number of cities: 5

Refer to the screenshot below of the executed example code for a better understanding.

How to Find the Number of Elements in a Python Array

In the above example, I initialized a variable count to keep track of the number of elements. Then iterate over the cities array using a for loop and increment the count variable for each element encountered. Then I printed the total count, which represents the number of elements in the array.

Read How to Print Duplicate Elements in Array in Python

Example

Let me tell you a real-world scenario: You are a data analyst working for a company in California, and you have an array of sales figures for different products. You want to determine the total number of products sold.

sales_figures = [1500, 2000, 1800, 3000, 2200, 2500]
product_count = 0
for sale in sales_figures:
    product_count += 1
print("Total number of products sold:", product_count)

Output:

Total number of products sold: 6

Look at the screenshot of the executed example code in the below.

Number of Elements in a Python Array Total number of products sold

Read 3D Arrays in Python

Use the range() Function

Python built-in range() function can also be used to determine the number of elements in an array. The range() function will generate a sequence of numbers, and by passing the length of the array as an argument, you can iterate over the indices of the array.

Here’s an example:

fruits = ["Apple", "Banana", "Orange", "Grape", "Mango"]
num_fruits = 0
for i in range(len(fruits)):
    num_fruits += 1
print("Number of fruits:", num_fruits)

Output:

Number of fruits: 5

In this example, I used the range() function in combination with the len() function to generate a sequence of indices for the fruits array. I iterate over the indices using a for loop and increment the num_fruits variable for each iteration. Then I printed the total count of fruits.

Check out How to Split a String into an Array in Python

Example

Here, I will tell you a scenario in which you are a software engineer working on a project for a company in Texas. You have an array containing the names of team members, and you want to assign each member a unique ID.

team_members = ["Sophia", "Jackson", "Mia", "Aiden", "Harper"]
num_members = 0
for i in range(len(team_members)):
    member_id = "TM" + str(i + 1)
    print("Member ID:", member_id, "- Name:", team_members[i])
    num_members += 1
print("Total number of team members:", num_members)

Output:

Member ID: TM1 - Name: Sophia
Member ID: TM2 - Name: Jackson
Member ID: TM3 - Name: Mia
Member ID: TM4 - Name: Aiden
Member ID: TM5 - Name: Harper
Total number of team members: 5

Conclusion

In this article, we explored all the ways to find the number of elements in a Python array. I explained what is an array in Python, We also discussed one of the frequently used methods len(), and understood how to Iterate and Count Elements, and also discussed how to use range() to find the number of elements in a Python array.

Hope this tutorial helped you to gain knowledge on arrays,len(), and range() and to learn how to find the number of elements in a Python array.

You may also like

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.