Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

append - (Begginer programmer) What is the difference between appending something to a list and inserting?

Given numbers [1,2,3,4} if I appended 4 what would the list now be?

Given the same list of numbers if I inserted 4 what would the result be.

Im new in programming (dont roast me lol) and am not grasping this concept yet.

question from:https://stackoverflow.com/questions/65946032/begginer-programmer-what-is-the-difference-between-appending-something-to-a-li

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Terms explained: append is putting the new item at the tail/end of an existing list/array. And insert is to put the new item at specific position or index. There are more info. you can read through to deepen your understanding here - https://docs.python.org/3/tutorial/datastructures.html

Let's have some examples to show the difference:

lst = [1, 2, 3, 4, 5]

x = 9

lst.append(x)    # lst becomes - [1, 2, 3, 4, 5, 9]

y = 11
lst.insert(3, y)    # lst now is - [1, 2, 3, 11, 4, 5, 9]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...