In Python 2.7 what is difference between var1 = [list(range(6))] and var2 =range(6)?

In ex32 of LPTHW I found that I could make a list by doing this:

var1 = [list(range(6))]


var2 = range(6)

But, when I print var1 in a for-loop using '%d' I get a typeError saying it wants a number not a list. Meanwhile var2 will print using '%d'. How did I construct var1 to require a specific format specifier?


Your mistake with var1 is that you actually have a 2d list. By enclosing the entire thing in square brackets you are actually creating an external list, with a single element. This is the cause of your error, as that one element is a list also.

>>> list(range(6))
[0, 1, 2, 3, 4, 5]
>>> [list(range(6))]
[[0, 1, 2, 3, 4, 5]]

Just get rid of the enclosing square brackets to make it work properly. Furthermore, as you are in Python 2, you don't need to do a list conversion, just do:


Which was your second option all along. :)

Let's iterate through some cases:


This produces a list [0, 1, 2, 3, 4, 5]


list() expects an iterable object, and will transform that into a list. So list(range(6)) takes the list returned by range(6), and copies its members into a new list. So we get [0, 1, 2, 3, 4, 5].


This uses the python list creation syntax. This will take each argument inside the brackets, and place them into a list. So we would get: [ [0, 1, 2, 3, 4, 5] ]. Note that this is a list of a list.


Here, we should know enough to know what happens. More or less, range(6), and list(range(6)) do the same thing, so we would expect the same result as the previous line, so we see: [ [0, 1, 2, 3, 4, 5] ].

Notes about python3.x

Python 3 has attempted to make more lazy objects or iterators returned by builtins. So range(6) doesn't return a list in python 3.x. Instead, it returns a custom range object. This object can be converted into a list by doing list(range(6)), because it's iterable. However, if you don't need to mutate the returned object, then it probably makes sense to leave it as a range object.

var1 = [list(range(6))] creates a list of a list, while var2 = range(6) creates a list.

>>> var1 = [list(range(6))]
>>> var1
[[0, 1, 2, 3, 4, 5]]
>>> var2 = range(6)
>>> list(var2)
[0, 1, 2, 3, 4, 5]

When you include a variable in python within square brackets, it returns a list with the included variable as an element of the list:

>>> var3 = 1
>>> var4 = [var3]
>>> isinstance(var4, list)

So list(range(6)) is all you need to be doing. It returns a list. Since you included that in square brackets by doing [list(range(6))], you made it a list of a list.

Need Your Help

Invoke Javascript file from C# application

c# javascript jquery .net

I have a .js file that performs a fairly simple task of building an array strings. Without going into too much detail, I have found Javascript the best language to perform this specific task.

Videoview in scrollview issue

android android-videoview android-scrollview

I have googled a lot to solve this issue but it doesn't works in any case. Have tried everything as mentioned in Android::VideoView inside a ScrollView and Android Scrollview having videoview is gi...