Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list vs [] with range
#1
Hello, I am just learning python so bear with me.

I am trying to figure out why the output of the following two lines are different despite both of them returning a list.

In: [range(9)]
Out: [range(0, 9)]
In: list(range(9))
Out: [0, 1, 2, 3, 4, 5, 6, 7, 8]

In: type([range(9)])
Out: list
In: type(list(range(9)))
Out: list

I am guessing for the 2nd approach I am passing a range object in a list constructor. Is my guess corrcet?
Reply
#2
You are correct. The first one has a single object in the list, the second one is the expansion of the range and has the ints inside.

>>> len([range(9)])
1
>>> type([range(9)][0])
<class 'range'>
>>> len(list(range(9)))
9
Reply
#3
In your example range(9) is a range object. A range object is an iterator that produces a sequence of integers. Wrapping [] around the range object makes it a list that contains a range object.

list(arg) is a function that creates a list. arg is an iterator that is used to generate elements for the list. Passing range(9) to list() uses range(9) as an iterator to generate integers zero through eight.
Reply
#4
range returns iterable object that produces sequence of numbers (from start to end). If you type help(list), you can see that list-constructur expects as its argument an iterable object. So, if you passed range object to list, list constructor would just unpack (traverse the iterable and build the list) content of the range object. However, if you type [range(9)], you put range object directly into the list (without unpacking). Note, you can also unpack range as follows: [*range(9)].
Reply
#5
(Sep-19-2020, 03:33 AM)deanhystad Wrote: A range object is an iterator that produces a sequence of integers.

There are iterables and iterators. Range is iterable but not iterator. Of course we can get iterator out of it as from any iterable but it doesn't change the fact that range is iterable and not iterator.

Iterators support next(), range does not; we can loop range without consuming it, range have length and supports indexing all of which is not possible with iterators.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Thanks for the clarification. I'll have to study that some more.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Down I hate "List index out of range" Melen 20 3,161 May-14-2023, 06:43 AM
Last Post: deanhystad
  IndexError: list index out of range dolac 4 1,845 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,411 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,501 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,111 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,762 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,239 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,186 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 6,350 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  IndexError: list index out of range ramu4651 2 3,694 Jan-24-2021, 01:45 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020