wilk94 wilk94 - 10 months ago 20
Python Question

How do I store the printed numbers from this function into a list (Python)?

a = 0
def multipleThree(n):
while a < n:
global a
a = a + 3
print (a, end= ' ')


This just prints out a bunch of numbers, but I want to save
into a list. I know that something like
listName = []
or something will probably be used. Thanks.


You can do this using a list comprehension:

def multipleThree(n):
    return [a for a in xrange(3, n + 4, 3)]

The xrange() here starts at 3, and ends at n+3, and increments by 3 on each iteration.

This will return a list.

You can then do this:

multiples = multipleThree