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 trial

Python

Joel Sprunger
Joel Sprunger
5,448 Points

python comprehensions: range() or list(range())?

I was trying to follow along in python 3.8 and range() wasn't working. I searched the web and found after 3.4 you need to use list(range()) to get the list rather than an object returned. I think this could be updated, even with just a note.

4 Answers

Steven Parker
Steven Parker
231,248 Points

It seems to work as expected for me:

>>> import sys
>>> sys.version
'3.8.0 (default, Nov 14 2019, 22:29:45)'
>>> [num * 2 for num in range(1, 6)]
[2, 4, 6, 8, 10]

Joel Sprunger
Joel Sprunger
5,448 Points

I'm sorry I thought this was connected to the python comprehensions workshop. Basically the syntax is shown as follows...

[num * 2 for num in range(1, 6)]

This wasn't working in python 3.8.

Steven Parker
Steven Parker
231,248 Points

It depends on what you are doing with it. By itself, "range" is not a list but it is usable as an iterable

For future questions, please show the actual code and provide a link to the course page you are working with.

Joel Sprunger
Joel Sprunger
5,448 Points

Okay it is working now. I'm really not sure what was different about my setup that was giving errors using range() but working using [range()]. Thanks for the help.