Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHailing Jin
363 Pointswhat does len(students_gpas) count? Is it the number of years on the first row or numbers of rows?
what does len(students_gpas) count? Is it the number of years on the first row or numbers of rows?If it is the number of years, then why do I get 3 instead of 4?
2 Answers
Boban Talevski
24,793 PointsIt is the number of rows indeed, which is why it returns 3. Technically, you ask how many elements this students_gpas array has? And it has 3 elements indeed. Each of them is another array each containing 4 elements, but len isn't concerned with that when it returns the result. It just sees 3 elements, it doesn't go deeper than that.
import numpy as np
some_array = np.array([
[1, 1, 1, 1, 1], # this is one element, who cares if it's a list or not, could be anything
[1, 1, 1, 1, 1], # this is another, so 2 in total
])
len(some_array)
So in this case len(some_array) returns 2. Hope that makes things clearer.
ursaminor
11,271 PointsI would guess it counts rows, but it's hard to know for sure without context. len() counts whatever iterable goes inside it. For example:
nums = [6, 3, 4, 77, 123]
len(nums)
5
So what is students_gpas? That's your answer.
Hailing Jin
363 PointsThank you!
Hailing Jin
363 PointsHailing Jin
363 PointsThank you for the clear explanation! It really helped me so much!