Why are [range (10)] and list (range (10)) not the same?

Why [range(10)]

and are list(range(10))

different in Python 3?

The output looks like this:

>>> print([range(10)])
[range(0, 10)]
>>> print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

      

+3


source to share


4 answers


Quoting the docs , "Lists can be constructed in several ways":

[range(10)]

      

creates a list of 1 element, a range object . In general, a comma-separated list of items enclosed in square brackets builds a list of the specified items.

list(range(10))

      



passes a range object as an argument to the list constructor:

class list([iterable])

The constructor builds a list whose elements match and are in the same order as the elements of the iterable. iterable can be either a sequence, an iterable container, or an iterator object. If the iterable is already a list, a copy is created and returned, similarly iterable[:]

. For example, list('abc')

returns ['a', 'b', 'c']

and list( (1, 2, 3) )

returns [1, 2, 3]

. If no argument is given, the constructor creates a new empty list []

.

A range in Python 3 is an immutable sequence of numbers , so in your case, the resulting list is a list of numbers from the range.

+11


source


[x]

means "create a list whose one item x

".

list(x)

means "create a list of which elements are elements x

."



range(10)

returns an object that is printed as range(0, 10)

(since it shows the initial value when it is printed) and whose elements are integers between 0 and 9, therefore [range(10)]

gives a list of one element [range(0, 10)]

and list(range(10))

gives a list of 10 elements [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

.

+5


source


In the first example, you simply create a list object using square brackets and place the object inside it range()

. On the other hand, in the second example, you are using the inline list()

to convert an object range()

to a list.

The difference is that using []

just creates a list. It does nothing for the content passed to. list()

, on the other hand, will either return an empty list if you don't pass anything or try to convert what you passed into a list object.

The documentation for list()

notes this
:

The constructor builds a list whose elements match and are in the same order as the elements of the iterable. iterable can be either a sequence, an iterable container, or an iterator object. If the iterable is already a list, a copy is created and returned, like an iterable [:]. For example, list ('abc') returns ['a', 'b', 'c'] and list ((1, 2, 3)) returns [1, 2, 3]. If no argument is given, the constructor creates a new empty list, [].

+1


source


range()

the function in python 3.x is similar to the function xrange()

in python 2.x. In Python 2.x range()

created a list and xrange()

returned an iterator - a sequence object

In python 2.x

>>> range(1)
[0]
>>> type(range(1))
<type 'list'>

      

In python 3.x

>>> range(1)
range(0, 1)
>>> type(range(1))
<class 'range'>

      

And to get the list, you can pass the generator to list()

>>> print (list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

      

For more information read here

-1


source







All Articles