-4
my_list = range(1, 11)

print(my_list[::2]) 

>>> range(1,11,2)

Its not displaying the correct output which should be [1, 3, 5, 7, 9]

1 Answers1

1

On python3.x, range returns a range object rather than a list. Slicing the range object just returns another range object which is what is displaying in your terminal:

>>> range(1, 11)[::2]
range(1, 11, 2)
>>> type(range(1, 11)[::2])
<class 'range'>

However, iterating over the result should produce the desired elements:

>>> list(range(1, 11)[::2])
[1, 3, 5, 7, 9]
mgilson
  • 300,191
  • 65
  • 633
  • 696