close

How to add element in Python to the end of list using list.insert?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to add element in Python to the end of list using list.insert in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to add element in Python to the end of list using list.insert?

  1. How to add element in Python to the end of list using list.insert?

    list.insert with any index >= len(of_the_list) places the value at the end of list. It behaves like append

  2. add element in Python to the end of list using list.insert

    list.insert with any index >= len(of_the_list) places the value at the end of list. It behaves like append

Method 1

You’ll have to pass the new ordinal position to insert using len in this case:

In [62]:

a=[1,2,3,4]
a.insert(len(a),5)
a
Out[62]:
[1, 2, 3, 4, 5]

Method 2

list.insert with any index >= len(of_the_list) places the value at the end of list. It behaves like append

Python 3.7.4
>>>lst=[10,20,30]
>>>lst.insert(len(lst), 101)
>>>lst
[10, 20, 30, 101]
>>>lst.insert(len(lst)+50, 202)
>>>lst
[10, 20, 30, 101, 202]

Time complexity, append O(1), insert O(n)

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read