Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 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 Reply

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]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...