Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list vs [] with range
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?
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)])
>>> type([range(9)][0])
<class 'range'>
>>> len(list(range(9)))
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.
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)].
(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.
Thanks for the clarification. I'll have to study that some more.

Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: list index out of range brunolelli 11 852 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  IndexError: list index out of range ramu4651 2 519 Jan-24-2021, 01:45 PM
Last Post: buran
  List index out of range error while accessing 2 lists in python K11 2 568 Sep-29-2020, 05:24 AM
Last Post: K11
  List index out of range error when attempting to make a basic shift code djwilson0495 4 791 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  TypeError indexing a range of elements directly on the list JFerreira 2 745 Mar-30-2020, 04:22 PM
Last Post: bowlofred
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 2,336 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  IndexError: List index out of range issue Adem 1 1,742 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  IndexError: list index out of range bug? CaptainCsaba 4 3,665 Oct-18-2019, 12:38 PM
Last Post: CaptainCsaba PRGM:IndexError: list index out of range bcramer 0 738 Aug-28-2019, 04:45 PM
Last Post: bcramer
  Preventing: IndexError: list index out of range PappaBear 1 4,948 Jun-03-2019, 05:50 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

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