Create a list from a sequence of numbers in Python

Viewed 41

I have written a code that returns the first 100 Fibonacci-numbers. Now, I want to make a list that contains all of those numbers, in square brackets and commas between all numbers, like a list. Obviously i can't just copy the returned numbers and paste them in a list with square brackets, since that won't give me any commas between the numbers.

What can I do?

2 Answers

Just append those numbers to a list the loop like my_list.append(<your fibonacci numbers>)

How does your function return the numbers?

If it takes an argument so that fib(n) returns the nth fibonacci number, you can use a comprehension to generate a list directly:

fiblist = [fib(n) for n in range(100)]
Related