2

I have a function called game() which prints out a number.

I need to run this 500 times using

for i in range(500):
    game()

but I cannot figure out how to convert this output of 500 numbers into a list.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
dan
  • 31
  • 1
  • 1
  • 3

1 Answers1

8

You can create a list, and append the output of game() into that list:

l = []

for i in range(500):
    l.append(game())

Or you can just use a list comprehension like below:

l = [game() for i in range(500)]

Note that if your game() function doesn't just return the value you want to put in a list, but print it out.

Then you have to change something like print(value) to return value in your game() function. Otherwise the list l will be full of None.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87