1

I have an int element list.

I want to change that list to a Fibonacci series sequence.

data = [1,0,2,4,5]
output_data = [1,1,3,7,12]
PiMathCLanguage
  • 363
  • 4
  • 15
rahul.m
  • 5,572
  • 3
  • 23
  • 50

2 Answers2

6

You can use itertools.accumulate:

import itertools as it

data = [1,0,2,4,5]
output = list(it.accumulate(data))

# [1, 1, 3, 7, 12]

The default binary function it applies is summation (more precisely, operator.add).

Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38
1
import itertools as it

data = [1,0,2,4,5]

l = [e for e in it.accumulate(data)]

print(l)
# output_data = [1,1,3,7,12]
Laurent B.
  • 1,653
  • 1
  • 7
  • 16