0

I have tried to add two numbers in the list. The result, generated was integer. I could like to use append function for list to add this integer to existing list. However, I am getting error the operation is not executable.

Fibo is a defined list

Fibo_FV = Fibo[i] + Fibo[i+1] 

print(Fibo_FV)
##result is sum of two numbers in the list

Fibo_final = Fibo.append(Fibo_FV)

print(Fibo_final)
##Answer is none

I am not sure why I am seeing none in when printed Fibo_final. My expectation is it should be new list with Fibo and newly added value concatenated into it. Any ideas on this one?

APhillips
  • 1,175
  • 9
  • 17
Pencil
  • 1
  • _I am not sure why I am seeing none in when printed Fibo_final_ The `.append()` method does not return anything, therefore you get `None` by default. `Fibo.append(Fibo_FV)` modifies the list in-place. – John Gordon Jan 15 '20 at 21:11

2 Answers2

0
Fibo_final = Fibo.append(Fibo_FV)

The function list.append modifies the list in place and returns None. The same is true for many other built-in operations, list.extend, dict.update, set.add, etc..

You can just do

Fibo.append(Fibo_FV)
print(Fibo)

instead

Cireo
  • 4,197
  • 1
  • 19
  • 24
0

Do these two separately as below

Fibo.append(Fibo_FV)
Fibo_final = Fibo
print(Fibo_final)

Why this happened is because the append is a function and it does not return anything, so your Fibo_final contains None.

High-Octane
  • 1,104
  • 5
  • 19