-1

Here, I have two examples, but I don't know why the two outcomes are different.

First example:

list_own=["subway", "plane", "ship","vehicle"]

list_own.insert(0,"train")

print(list_own)
['train', 'subway', 'plane', 'ship', 'vehicle']

Second example:

list_own=["subway", "plane", "ship","vehicle"]

print(list_own.insert(0,"train")
None

Does anyone know why the outcomes shown above are different?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Ryan
  • 21
  • 2

2 Answers2

3

Calling the insert method on list_own inserts the element into the list the way you expected, but itself returns None.

Chris
  • 26,361
  • 5
  • 21
  • 42
0

As stated in Python Documentation - Data Structures insert method of list modifies the original list but returns default value.

You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed – they return the default None

Which means when the modified list is printed, contents of the list gets printed but when insert method is called inside print then what insert returns i.e None that gets printed