Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range() in python 2.7 v. 3

Tags:

python

range

Just wondering how to get the same functionality out of range() that you get in python 2.7 in version 3?

In python 2.7:

>>> range(5)
[0, 1, 2, 3, 4]

In python 3:

>>> range(5)
range(0, 5)

I need to get a list that looks like the one above, but am restricted to using python3 for an assignment...

Thanks so much!

like image 348
goldisfine Avatar asked Jul 08 '26 01:07

goldisfine


1 Answers

Simply do this:

list(range(5))
=> [0, 1, 2, 3, 4]

In Python 3, range() returns an iterable of objects, but it's easy to convert it to a list, as shown above.

like image 169
Óscar López Avatar answered Jul 09 '26 17:07

Óscar López



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!