Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python __len__() magic method
Next article icon

Finding the length of an array inPython means determining how many elements are present in the array. For example, given an array like [1, 2, 3, 4, 5], you might want to calculate the length, which is 5. Let's explore different methods to efficiently.

Using len()

len() function is the most efficient way to get the number of elements in a list. It's implemented in C internally and returns the length in constant time.

Python
a=[1,2,3,4,5]res=len(a)print(res)

Output
5

Explanation: len(a) to count the number of elements in the lista, stores the result inres.

Using sum()

This method counts each element by summing 1 for every item in the list using a generator expression. It’s useful for iterables when you can’t use len().

Python
a=[1,2,3,4,5]res=sum(1for_ina)print(res)

Output
5

Explanation: sum(1 for _ in a)add 1 for each element in the lista, effectively counting the total number of items.

Using For loop

You manually loop through each element and increment a counter variable. While simple and readable, it’s not as concise as built-in functions.

Python
a=[1,2,3,4,5]res=0for_ina:res+=1print(res)

Output
5

Explanation: This code sets res to 0 and increments it by 1 for each item in the lista, counting the total elements.

Using enumerate()

The enumerate()function adds a counter to the iterable. You can get the length by capturing the last index it provides. You must start from 1 to match the actual count.

Python
a=[1,2,3,4,5]fori,_inenumerate(a,1):passres=iprint(res)

Output
5

Explanation: enumerate(a, 1)loop through the listawith a counter starting at 1. Although the loop does nothing (pass), the final value of i gives the total number of elements.

What is the Difference Between a Python Array and a List?

Let's understand the difference between a Python Array and a List. Both store collections of items but differ in data type, memory efficiency, operations and performance. Knowing these differences helps you choose the right structure based on your needs.

Parameter

Python Array

Python List

Data Type Constraint

Homogeneous (same data type)

Heterogeneous (different data types)

Memory Efficiency

More memory-efficient (contiguous memory)

Less memory-efficient (dynamic with extra features)

Supported Operations

Basic operations (indexing, slicing, appending)

Wider range (insertions, deletions, sorting, reversing)

Performance

Better performance for large, homogeneous data (numerical computations)

Slightly lower performance due to flexibility

Related Articles


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp