You can just construct a list from the range object:
my_list = list(range(1, 1001))
This is how you do it with generators in python2.x as well. Typically speaking, you probably don't need a list though since you can come by the value of my_list[i]
more efficiently (i + 1
), and if you just need to iterate over it, you can just fall back on range
.
Also note that on python2.x, xrange
is still indexable1. This means that range
on python3.x also has the same property2
1print xrange(30)[12]
works for python2.x
2The analogous statement to 1 in python3.x is print(range(30)[12])
and that works also.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…