Python Tips and Tricks

Python Tips and Tricks

  1. How to insert elements in the list?

i)beginning of the list

ii)end of the list

iii)at a particular index

s=[1,2,3,4,5,6]

Adding element at beginning of the list:

s.insert(0,99)

Adding element at the end of the list.

s.append(55)

Adding element at a particular index 3

s.insert(3,77)

List object in memory

Adding element at the beginning of the list.

Inserting an element at a particular index

Adding element at the end of the list

Note:

Adding element at end of the list using insert.

It will push the last element to the end of the list and insert it in that position.

s=[1,2,3]
s.insert(-1,99)
print (s)
Output:
[1, 2, 99, 3]

Leave a Reply