FAQ: Working with Lists in Python - Length of a List

This community-built FAQ covers the “Length of a List” exercise from the lesson “Working with Lists in Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Length of a List

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

list1 = range(2, 20, 2)
We have studied befour that list function return an object,so how does
list1_len=len(list1)
return the lenght of the list.
shouldn’t we write the above statement like :
list1_len=len(list(list1))

2 Likes

In Python 3 range() returns a range object which behaves like an iterable (such as a list). The object is not a list, proper, but it does have a length so we don’t need to re-cast it to list to obtain that information, and neither do we need to do that if we just want to iterate it with for, or any other iterator.

>>> len(range(10))
10
>>> 

and,

for x in range(2, 20, 2):
    print (x)

output

2
4
6
8
10
12
14
16
18
3 Likes

What operations can be done on a list vs a range data type in Python?

list is just an iterable. Nothing special, though some methods only work for lists (they’re attributes of that class). A range is not a list but it behaves almost like one.

>>> a = 1
>>> b = 20
>>> [c for c in zip(range(a, b+1), range(b, -a, -1))]
[(1, 20), (2, 19), (3, 18), (4, 17), (5, 16), (6, 15), (7, 14), (8, 13), (9, 12), (10, 11), (11, 10), (12, 9), (13, 8), (14, 7), (15, 6), (16, 5), (17, 4), (18, 3), (19, 2), (20, 1)]
>>> 

The above only took up memory long enough to echo it to the console. After that it’s gone. What’s more, the zip object doesn’t take up any memory, either, beyond what the two ranges take up, which is fixed, and a very small amount. We leverage this in the comprehension and have the option of keeping the whole list, or accessing one element then letting it go. If it’s a not very often process, then go this route. If there are a lot of repeated calls, then cache the output (assign it to a variable) and poll that variable on each call.

If our range is dynamic then ixnay hattay deaiay. We need to generate a new collection each time it is accessed. No problem, so long as lists are not involved. The comprehension is enough. These are design constraints we have control over. Use as few resources as possible and you’re half the way toward optimization. After that it’s all in the algorithms we choose.

Don’t know if anyone caught this… All the value pairs add up to a + b.

Maybe a good idea to change the variable names both to *_len or to *_length in the exercise.

I’m more annoyed at the persnickety level… Like why do I have to write two separate lines for len and print? It’s valid code dear code sandbox: `
This:
big_range_length= print(len(big_range))

Instead of:
“big_range_length = len(big_range)
print(big_range_length)”

Tell me that’s not something a full grown PyDev wouldn’t write. Chain all the commands together…

Hmm, can’t, they wouldn’t.

1 Like

I mean the point stands though you ought to be able to chain the commands together ? Two lines for something that should take one ?! and something that was even in CC example code too when they were explaining the length attribute? Why not encourage it ??

1 Like

Encourage assigning, print() to a variable?

1 Like

Okay, I’m going to be the idiot in the room here with this.

big_range = range(2, 3000, 10)

Now reading it, it should start at the number 2 and count up by 10’s till it hits the closest it can to 3000, correct?

My issue is that in my stupid brain I am thinking it would be around 299 or 299.2 or something to that effect cause the highest it can hit is 2992 and you divide that by 10 and get 299.2 but not 300 cause for 300 to be correct it would need to be going to 3002. Can someone tell me where my brain is going wrong?

Or am I being stupid and not counting 2 at the start?

range(2, 3000, 10)
#   start stop step

[2, 12, 22, 32, 42, …, 2972, 2982, 2992]

start is inclusive
stop is excluded
step defaults to 1 when we do not include it in the arguments

Not sure what your goal is. Can you describe it a little more?

long_list = [1, 5, 6, 7, -23, 69.5, True, "very", "long", "list", "that", "keeps", "going.", "Let's", "practice", "getting", "the", "length"] big_range = range(2, 3000, 10) # Your code below: long_list_len = len(long_list) print(long_list_len) big_range_length = len(big_range) print(big_range_length)

I was running this part of the python stuff and when I printed big_range_length it gave me 300 which I stopped and was like shouldn’t it be 299. I am wondering if I am just miscounting or if the program rounded up since 2992 (the largest it could hit) divided by 10 is 299.2 Sorry if I am making little sense.

1 Like
>>> len([*range(2, 3000, 10)])
300

Our figuring may not always be correct, especially if assumptions are in play. Think, 0..10 will have 11 terms if 10 is included. 0..100 by tens will also have 11 terms if 100 is included.

1 Like

Okay so I think I follow now. the zero place is also counted making the count 300 then.

So pretty much this.
2+(10*N) <= 3000
10 * N <= 3000 - 2
10 * N <= 2998
N <= 299.8
N <= 299
N = [0,299]
So 300 total?

1 Like

Hi everyone.
Why I,m getting the same results for Length? in ** range(2, 3000, 10)** and ** range(2, 3000, 100)**

ong_list = [1, 5, 6, 7, -23, 69.5, True, “very”, “long”, “list”, “that”, “keeps”, “going.”, “Let’s”, “practice”, “getting”, “the”, “length”]

big_range = range(2, 3000, 10)

Your code below:

long_list_len = len(long_list)

print(long_list_len)

big_range_length = len(big_range)

print(big_range_length)

big_range = range(2, 3000, 100)

print(big_range_length)

18
300
300

To preserve code formatting in forum posts, see: [How to] Format code in posts

The statements are executed top to bottom. You have re-assigned the new range to the big_range variable. But you haven’t re-calculated the length and then re-assigned the result to the big_range_length variable.

big_range = range(2, 3000, 10)
big_range_length = len(big_range)

print(big_range_length)
# 300

big_range = range(2, 3000, 100)
big_range_length = len(big_range) # You are missing this statement

print(big_range_length)
# 30

I am not understanding the question in this lesson. I have tried to rename the original list, long_list, to save the len() value and list to the new variable, long_list_len.
Can you help me understand the question??

long_list = [1, 5, 6, 7, -23, 69.5, True, “very”, “long”, “list”, “that”, “keeps”, “going.”, “Let’s”, “practice”, “getting”, “the”, “length”]

Renaming long_list to long_list_len

long_list_len = long_list
#find number of items in list
print(long_list_len)
print(len(long_list_len)

In real world terms, what is a list? Something we would take to the store, perhaps? Or tasks to get done today? Or items in a bolt bin, aka any inventory collection, itemized? In other words, nothing out of the ordinary. We’ve used lists all of our lives.

Sometimes a shopping list can have options, such as pears or mangos, or red or brown rust paint. This is where we add dimensions to our lists. Try and visualize that.

Skip the above if you are still getting your bearings on an itemized list. It will come up later and is not important, just now. A list written on a sheet of paper is one thing, but when we add structure, such as throwing names into a hat, we embrace a greater meaning. The hat is the means to structuring the names.

That means we are not confined to tossing just names into the hat, we can toss coins, dice, golf balls, or anything else that will fit into the hat. The structure can hold a lot of different things, not just names on a strip of paper.

Visualize this. Now look at this structure of heterogeneous items and count them. Their shape and size means nothing, only how many we can count. That is the length of the list.

When there is structure, there is accountability. We can count things, as in, enumerate them. Once we know that about a structure, we can access all of its elements, either by counting, or direct access. This is the basic mechanics of lists. You know most of the details already, We’re only adding to it so you can make good use of this object in Python.

long_list_len = len(long_list)
                  ^
              function to
              return length
               (item count)

I am not having much success getting a check for the program I have written. I’ve reinterpreted the question. I’ve tried setting the variable ‘long_list_len’ equal to the number 18, or the length of the long_list.
Somehow, I am not getting the concept.
See my program below:

long_list = [1, 5, 6, 7, -23, 69.5, True, “very”, “long”, “list”, “that”, “keeps”, “going.”, “Let’s”, “practice”, “getting”, “the”, “length”]
print(len(long_list))

Renaming long_list to long_list_len

#find number of items(long_list_len))
long_list_len = 18
print(long_list_len)
long_list_len = range(2, 20, 2)
print(list(long_list_len))
print(long_list_len)
print(len(long_list_len))
big_range = range(2, 3000, 10)
print(big_range)

print(len(big_range))

I’m not getting the concept. Do you have suggestions?