Midavalo Midavalo - 4 months ago 9
Python Question

Create list of sequential numbers until next rounded number past x

I'm trying to create a list of sequential numbers in python from 1 to a specified number, but need to round the numbers off to one that's divisible by 10.

e.g. if my specificed number is

7
my list would be
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


I've written the following code, which does work, but I'm unsure whether this is the best/most efficient way to write it, particularly if my numbers get large.

x = 33
i = 1
mylist = list()

while i <= x:
mylist.append(i)
i += 1

while mylist[len(mylist)-1] % 10 != 0:
mylist.append(i)
i += 1


which would output:

print mylist
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]


Is there another better or more efficient (to run) way to write this?

Answer
def makeList(n):
    return range(1, n + 11 - (n % 10))
Comments