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.
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.
In Pythons <= 3.4 you can, as others suggested, use list(range(10)) in order to make a list out of a range (In general, any iterable).
Another alternative, introduced in Python 3.5 with its unpacking generalizations, is by using * in a list literal []:
>>> r = range(10)
>>> l = [*r]
>>> print(l)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Though this is equivalent to list(r), it's literal syntax and the fact that no function call is involved does let it execute faster. It's also less characters, if you need to code golf :-)
Python In Excel Convert Range to List
Python range to list - Stack Overflow
How to convert numeric string ranges to a list in Python - Stack Overflow
Convert range(r) to list of strings of length 2 in python - Stack Overflow
Hi Everyone, I am trying to use Python In Excel to perform a multiple criteria filter on a dataframe but it keeps returning an empty dataframe. I suspect that the xl function cannot read a range as a list, regardless of what I do. I'm using the code below, am I doing something wrong?
filt = office_supplies['Rep'].isin([xl("C2:C3", to_list = True)])
office_supplies[filt]
You can just assign the range to a variable:
range(10)
>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In your case:
>>> nums = range(9000,9004)
>>> nums
[9000, 9001, 9002, 9003]
>>>
However, in python3 you need to qualify it with a list()
>>> nums = list(range(9000,9004))
>>> nums
[9000, 9001, 9002, 9003]
>>>
Python 3
For efficiency reasons, Python no longer creates a list when you use range. The new range is like xrange from Python 2.7. It creates an iterable range object that you can loop over or access using [index].
If we combine this with the positional-expansion operator *, we can easily generate lists despite the new implementation.
[*range(9000,9004)]
Python 2
In Python 2, range does create a list... so:
range(9000,9004)
def f(x):
result = []
for part in x.split(','):
if '-' in part:
a, b = part.split('-')
a, b = int(a), int(b)
result.extend(range(a, b + 1))
else:
a = int(part)
result.append(a)
return result
>>> f('1,2,5-7,10')
[1, 2, 5, 6, 7, 10]
I was able to make a true comprehension on that question:
>>> def f(s):
return sum(((list(range(*[int(j) + k for k,j in enumerate(i.split('-'))]))
if '-' in i else [int(i)]) for i in s.split(',')), [])
>>> f('1,2,5-7,10')
[1, 2, 5, 6, 7, 10]
>>> f('1,3-7,10,11-15')
[1, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15]
the other answer that pretended to have a comprehension was just a for loop because the final list was discarded. :)
For python 2 you can even remove the call to list!
Use string formatting and list comprehension:
>>> lst = range(11)
>>> ["{:02d}".format(x) for x in lst]
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
or format:
>>> [format(x, '02d') for x in lst]
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
zfill does exactly what you want and doesn't require you to understand an arcane mini-language as with the various types of string formatting. There's a place for that, but this is a simple job with a ready-made built-in tool.
ranger = [str(x).zfill(2) for x in range(r)]