Python 3 Turn Range to a List

Python range to list

Since you are taking the print statement under the for loop, so just placed the print statement out of the loop.

nums = []
for x in range (9000, 9004):
nums.append(x)
print (nums)

Convert string of integer ranges into a list

You can use re module for the task.

For example:

import re

strings = ["1-4, 7, 9-11",
"5-9",
"1,2,3,10,11",
"1-3,5,6,7"]

for s in strings:
out = []
for a, b in re.findall(r'(\d+)-?(\d*)', s):
out.extend(range(int(a), int(a)+1 if b=='' else int(b)+1))
print('Input = {:<20} Output = {}'.format(s, out))

Prints:

Input = 1-4, 7, 9-11         Output = [1, 2, 3, 4, 7, 9, 10, 11]
Input = 5-9 Output = [5, 6, 7, 8, 9]
Input = 1,2,3,10,11 Output = [1, 2, 3, 10, 11]
Input = 1-3,5,6,7 Output = [1, 2, 3, 5, 6, 7]

python 3 list from range

range doesn't do fractions. Also the third argument is the step, that is:

range(start, stop, step)

To get what you want with fractions you can use numpy arrays. For example:

import numpy as np
x = np.linspace(0, 10, 100)

Print a list in reverse order with range()?

use reversed() function:

reversed(range(10))

It's much more meaningful.

Update:

If you want it to be a list (as btk pointed out):

list(reversed(range(10)))

Update:

If you want to use only range to achieve the same result, you can use all its parameters. range(start, stop, step)

For example, to generate a list [5,4,3,2,1,0], you can use the following:

range(5, -1, -1)

It may be less intuitive but as the comments mention, this is more efficient and the right usage of range for reversed list.

Convert a string to a list of numbers in python 3

You can use a nested list comprehension, checking whether the current part contains a - and using either a range or creating a one-elemented list accordingly:

>>> s = "1-10, 20, 30, 40, 400-410"
>>> [n for part in s.split(", ") for n in (range(*map(int, part.split("-"))) if "-" in part else [int(part)])]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 30, 40, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409]

Maybe split this up, for readability:

>>> to_list = lambda part: range(*map(int, part.split("-"))) if "-" in part else [int(part)]
>>> [n for part in s.split(", ") for n in to_list(part)]

Note: As in your first example, this will translate "1-10" to [1, 2, ..., 9], without 10.


As noted in comments, this will not work for negative numbers, though, trying to split -3 or -4--2 into pairs of numbers. For this, you could use regular expressions...

>>> def to_list(part):
... m =re.findall(r"(-?\d+)-(-?\d+)", part)
... return range(*map(int, m[0])) if m else [int(part)]
...
>>> s = "-10--5, -4, -2-3"
>>> [n for part in s.split(", ") for n in to_list(part)]
[-10, -9, -8, -7, -6, -4, -2, -1, 0, 1, 2]

... or just use a different delimiter for ranges, e.g. -10:-5.

Range function does not print list of numbers

This is a range object. If you want a list with numbers 1 to 1000 with 2 as steps in it you can do like this:

list(range(1, 1000, 2))

If you don't want to change the values in the list, using tuple is a better option:

tuple(range(1, 1000, 2))

Range object is different from a list. It doesn't actually contain numbers from 1 to 1000. When you use it in a for loop it generates numbers as it loops through.

For example if you create a range from one to then thousand its not gonna take a lot of memory; but when you convert it to a list, that's when all the actual numbers are gonna be stored in the memory.

In Python 2, range would return a list, but in Python 3 range is an immutable sequence of type range. As stated in python documents:

The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start, stop and step values, calculating individual items and subranges as needed).

But this doesn't mean you can't use indexing for a range object. Since they are immutable sequences, you can apply indexing on them the same way as a list. The return value will be a range object as well (unless its not sliced and it's only selecting one single element, then it would return that number as int).

Convert range(r) to list of strings of length 2 in python

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']

How do I create a list with numbers between two values?

Use range. In Python 2, it returns a list directly:

>>> range(11, 17)
[11, 12, 13, 14, 15, 16]

In Python 3, range is an iterator. To convert it to a list:

>>> list(range(11, 17))
[11, 12, 13, 14, 15, 16]

Note: The second number in range(start, stop) is exclusive. So, stop = 16+1 = 17.


To increment by steps of 0.5, consider using numpy's arange() and .tolist():

>>> import numpy as np
>>> np.arange(11, 17, 0.5).tolist()

[11.0, 11.5, 12.0, 12.5, 13.0, 13.5,
14.0, 14.5, 15.0, 15.5, 16.0, 16.5]

See: How do I use a decimal step value for range()?

Python range( ) is not giving me a list

You're using Python 3, where range() returns an "immutable sequence type" instead of a list object (Python 2).

You'll want to do:

def RangeTest(n):
return list(range(n))

If you're used to Python 2, then range() is equivalent to xrange() in Python 2.


By the way, don't override the list built-in type. This will prevent you from even using list() as I have shown in my answer.



Related Topics



Leave a reply



Submit