## Python - Create list with numbers between 2 values?

### Question

How would I create a list with values between two values I put in? For example, the following list is generated for values from 11 to 16:

``````list = [11, 12, 13, 14, 15, 16]
``````
2019/02/12
1
380
2/12/2019 1:33:45 AM

Use `range`. In Python 2.x it returns a list so all you need is:

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

In Python 3.x `range` is a iterator. So, you need to convert it to a list:

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

Note: The second number is exclusive. So, here it needs to be `16+1` = `17`

EDIT:

To respond to the question about incrementing by `0.5`, the easiest option would probably be to use 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]
``````
2019/10/06
685
10/6/2019 11:37:03 PM

You seem to be looking for `range()`:

``````>>> x1=11
>>> x2=16
>>> range(x1, x2+1)
[11, 12, 13, 14, 15, 16]
>>> list1 = range(x1, x2+1)
>>> list1
[11, 12, 13, 14, 15, 16]
``````

For incrementing by `0.5` instead of `1`, say:

``````>>> list2 = [x*0.5 for x in range(2*x1, 2*x2+1)]
>>> list2
[11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0]
``````
2013/08/16

Try:

``````range(x1,x2+1)
``````

That is a list in Python 2.x and behaves mostly like a list in Python 3.x. If you are running Python 3 and need a list that you can modify, then use:

``````list(range(x1,x2+1))
``````
2013/08/16

If you are looking for range like function which works for float type, then here is a very good article.

``````def frange(start, stop, step=1.0):
''' "range()" like function which accept float type'''
i = start
while i < stop:
yield i
i += step
# Generate one element at a time.
# Preferred when you don't need all generated elements at the same time.
# This will save memory.
for i in frange(1.0, 2.0, 0.5):
print i   # Use generated element.
# Generate all elements at once.
# Preferred when generated list ought to be small.
print list(frange(1.0, 10.0, 0.5))
``````

Output:

``````1.0
1.5
[1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5]
``````
2017/10/24

Use list comprehension in python. Since you want 16 in the list too.. Use x2+1. Range function excludes the higher limit in the function.

list=[x for x in range(x1,x2+1)]

2013/08/16

assuming you want to have a range between x to y

``````range(x,y+1)

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

use list for 3.x support

2013/08/16