0

I'm trying to create a list from a range, but when I stick it in another list I get a class and not the list of lists that I'm expecting - [[0,1,2]]

Code:

    initial_list = range(3)
    list_of_lists = [initial_list]
    for i in list_of_lists:
    print(i, type(i))

This outputs range(0,3), <class 'range'> However, I want [[0,1,2]] Does anyone know how to force range to expand?

Micah Pearce
  • 1,805
  • 3
  • 28
  • 61

2 Answers2

2

This answer explains that you're pretty close! If you were in Python 2.x, this approach would have worked, since in 2.x, range() returns a list. In 3.x, however, range() returns an iterator.

Expand it with list(range(3)) to get the desired result.

Nick Reed
  • 4,989
  • 4
  • 17
  • 37
1

You could append it to anotehr list. Heres what i've done do to this:

initial_list = []
for i in range(3):
 initial_list.append(i)
list_of_lists = [initial_list]

Then in IDLE it Showed:

list_of_lists
[0, 1, 2]]
Antoine Delia
  • 1,728
  • 5
  • 26
  • 42
Nifri
  • 11
  • 2