0

I want to print a list of odd numbers from 1 to 1000.

x = range(1, 1000, 2)
print (x)

The above code prints: range(1, 1000, 2). However, when I use a for loop it works fine.

Can anybody explain why?

john-hen
  • 4,410
  • 2
  • 23
  • 40
Avinash K
  • 43
  • 5
  • 4
    That's because `x` is a `range` object, not a `list`. You can convert it though: `x = list(range(1, 1000, 2))` – user2390182 May 16 '21 at 17:24
  • 1
    Are you perhaps trying to learn Python from a source that covers Python 2? In Python 2, `range` did return a list. You should note that all versions of Python 2 have been [officially deprecated since January 2020](https://www.python.org/doc/sunset-python-2/), and will no longer receive maintenance or security updates. – Brian61354270 May 16 '21 at 17:29
  • Documentation: [`range`](https://docs.python.org/3/library/stdtypes.html#ranges) – wjandrea May 16 '21 at 17:38
  • 1
    Does this answer your question? [Python 3 turn range to a list](https://stackoverflow.com/questions/11480042/python-3-turn-range-to-a-list) – aaronn May 26 '21 at 11:17

3 Answers3

2

This is a range object. If you want a list with numbers 1 to 1000 with 2 as steps in it you can do like this:

list(range(1, 1000, 2))

If you don't want to change the values in the list, using tuple is a better option:

tuple(range(1, 1000, 2))

Range object is different from a list. It doesn't actually contain numbers from 1 to 1000. When you use it in a for loop it generates numbers as it loops through.

For example if you create a range from one to then thousand its not gonna take a lot of memory; but when you convert it to a list, that's when all the actual numbers are gonna be stored in the memory.

In Python 2, range would return a list, but in Python 3 range is an immutable sequence of type range. As stated in python documents:

The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start, stop and step values, calculating individual items and subranges as needed).

But this doesn't mean you can't use indexing for a range object. Since they are immutable sequences, you can apply indexing on them the same way as a list. The return value will be a range object as well (unless its not sliced and it's only selecting one single element, then it would return that number as int).

aaronn
  • 448
  • 1
  • 6
  • 16
  • `range.__getitem__` returns a `range` if its argument is a `slice` object; otherwise it returns a single `int`. – chepner May 16 '21 at 18:51
  • @chepner you are right. If a single element of `range` is picked by index, it will return that integer. range(10)[2] == 2 – aaronn May 16 '21 at 18:59
1

Try it out.

>>> 
>>> x = range(1, 10, 2)
>>> 
>>> print (type(x))
<class 'range'>
>>> 
>>> print(list(x))
[1, 3, 5, 7, 9]
>>> 
>>> 

In python 3.x range returns object while in python 2.x it returns list by default.

Chandella07
  • 2,089
  • 14
  • 22
0

x is a range object.
try this code instead

x = list(range(1, 1000, 2))
print(x)
George Imerlishvili
  • 1,816
  • 2
  • 12
  • 20