-2

When I run this code, it gives None instead of the 'Python' getting appended to the List. Why is that so?

input_tuple = ('Monty Python', 'British', 1969)  
input_list = list(input_tuple)  
print(input_list)  
input_list_1 = input_list.append('Python')  
print(input_list_1)
dda
  • 6,030
  • 2
  • 25
  • 34
VikB
  • 1
  • 1
  • 3
    You are calling an object function here (right side of =): `input_list_1 = input_list.append('Python')`. No value is returned. – Anton vBR Feb 18 '18 at 09:02

4 Answers4

0

This is the offending line:

input_list_1 = input_list.append('Python')

The append() method updates the list in place. It returns no value (i.e. it returns None). Since you are assigning the return value of append() to input_list_1, the printed value is expected to be "None".

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
0

array.append() changes the array passed directly instead of returning the changed array. To give an example:

array = [1, 2, 3]
array.append(4)
print(array)

Outputs [1, 2, 3, 4]. Therefore, your code should be:

input_tuple = ('Monty Python', 'British', 1969)
input_list = list(input_tuple)
print(input_list)
input_list.append('Python')
print(input_list)
-1

input_list_1 returns None because you are appending into input_list. At this point input_list_1 stores returned value of append operation - which is None. To print updated list you should print the input_list.

Peter R.
  • 105
  • 10
-1

input_list is list object and append is a list object method which is direct changes in list instant of return changes.that's why none object is returned and input_list_1 does not print anything.