Python – Generate list of doubles

In this post we will be creating a Python script that will generate a list of doubles and return this to output. The sequence will consist of numbers being added by two each time

See the sample of Python code below where generate a list of doubles. In this example we ask the user to provide a maximum length for the sequence, this is then print to output.

def double(x):
    return x * 2

while True:
    number=int(input("Enter maximum range: "))
    doubles = map(double, range(number))
    print(list(doubles))

An example of output can be seen below.

Enter maximum range: 5
[0, 2, 4, 6, 8]

Enter maximum range: 10
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Enter maximum range: 20
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]
>>>

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply