wilk94 wilk94 - 1 year ago 59
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.

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download