What does list.insert() in actually do in python?
问题 I have code like this: squares = [] for value in range(1, 5): squares.insert(value+1,value**2) print(squares) print(squares[0]) print(len(squares)) And the output is : [1, 4, 9, 16] 1 4 So even if I ask python to insert '1' at index '2', it inserts at the first available index. So how does 'insert' makes the decision? 回答1: From the Python3 doc: list.insert(i, x) Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts