Advanced Python Code Challenges: Lists - 1. Every Three Numbers


I am currently working on the above exercise and I cant seem to get the list to generate, am i missing something?

I bet its something really silly.

as you can see on the right hand side, range() does not return a list

we could convert the range object into a list.

It’s not silly.

You are missing one component, a constructor before range()

Hi, thanks,

Could you show me using this example of how the list would be structured?

def every_three_nums(start):
          return list([range(start, 101, 3)])

I have tried to apply a list to the return line, but I am getting an typeerror.

So first you wrap the range object in a list, and then you try to convert the list into a list? Why?

1 Like

Ok sussed it, got my brackets muddled up.

def every_three_nums(start):
   return list((range(start, 101, 3)))

I would still argue you have on set of parentheses too many

1 Like

I an not entirely sure what you mean, please understand I am still learning and I don’t know all the technical jargon yet.

parentheses/brackets, these things: ()

isn’t such a technical term, right?

you have 3 sets of parentheses, while you only need two. the “middle” set of parentheses doesn’t serve any purpose at the moment

1 Like

first, you should check with an if statement if start is greater than 100 or not, if true do empty list, else do a list with the range (start, stop, step ).

1 Like

I think what you missed here is that range() function doesn’t return a list. It returns a special range object.

It will not return a list containing elements from 0 to 5. Instead, you’ll get an output such as

But, if you try:

the output here will be:
[0, 1, 2, 3, 4]

So, range() returns a special range object but a list is never explicitly created with it. Because of this reason, python is able to save memory while running loops because it doesn’t create an explicit array.