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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s