Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

How might I create a list with values between two values I put in? For instance, the accompanying list is produced for values from 11 to 16:

list=[11,12,13,14,15,16]

1 Answer

0 votes
by (26.4k points)
edited by

You can use range.

In python 2.x, it will returns a list:

>>> range(11, 17)

[11, 12, 13, 14, 15, 16]

In python 3.x, this range will be a iterator. So, you want to convert it into list:

>>> list(range(11, 17))

[11, 12, 13, 14, 15, 16]

Here, the second number is exclusive. So, you have to take number like 16+1=17

You can also use arange() and .tolist() from numpy

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

Interested to learn python in detail? Join the python training course to gain more knowledge.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...