Append, Update and Delete elements in a list

This blogs explain how to update, append and delete element from a list

List are mutable which means elements inside a list can be modified
Elements can be deleted, appended or updated
Appending an elements means adding a new element at the end of a list
Use append() function to add new element at the end of a list

Append Example

Creating a number list and printing the original elements inside a list

num = [1,2,3,4,5,6,7]
print('Original list')
print(num)

Using append() function to add 3 new elements at the end of the list

num.append(8)
num.append(9)
num.append(10)

Printing elements inside a list

print(num)

Output

Original list
[1, 2, 3, 4, 5, 6, 7]
Appending 3 new elements
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Complete Code

num = [1,2,3,4,5,6,7]
print('Original list')
print(num)
num.append(8)
num.append(9)
num.append(10)
print('Appending 3 new elements')
print(num)

Update the element

Update means changing the value of the element inside a list
It is done using indexing or slicing and assigning a new value

Update Example

Creating a num list

num = [1,2,3,4,5,6,7]
print('Original list')

Now using index number of the element to change the value. We will be changing values of some of the element using their index number

num[0]=10
num[3]=30

Output

Original list
[1, 2, 3, 4, 5, 6, 7]
After updating some of the elements
[10, 2, 3, 30, 5, 6, 7]

Complete Code

num = [1,2,3,4,5,6,7]
print('Original list')
print(num)
num[0]=10
num[3]=30
print('After updating some of the elements')
print(num)

Elements can also be updated using slicing

Slicing example

Create a num list

num = [1,2,3,4,5,6,7]

Using slicing to update elements. Below statement will update third and the fourth element

num[2:3] = 30,40

Output

Original list
[1, 2, 3, 4, 5, 6, 7]
After updating some of the elements using slicing
[1, 2, 30, 40, 4, 5, 6, 7]

Complete Code

num = [1,2,3,4,5,6,7]
print('Original list')
print(num)
num[2:3] = 30,40
print('After updating some of the elements using slicing')
print(num)

Deleting element

Element can be deleted using del statement. It takes the position number of the element that has to be deleted

Example

num = [1,2,3,4,5,6,7]
print('Original list')
print(num)
del num[1]
print('After deleting element')
print(num)

Output

Original list
[1, 2, 3, 4, 5, 6, 7]
After deleting element
[1, 3, 4, 5, 6, 7]

There is also an another way of deleting element from a list is by using remove() method. This method takes the element that has to be deleted

num = [1,2,3,4,5,6,7]
print('Original list')
print(num)
num.remove(2)
print('After deleting element')
print(num)

Output

Original list
[1, 2, 3, 4, 5, 6, 7]
After deleting element
[1, 3, 4, 5, 6, 7]

Advertisement

Leave a Comment

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 )

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