Sayantan's Blog On Python Programming

REMOVING ELEMENT IN THE LISTS

REMOVING ELEMENT IN THE LISTS

INTRODUCTION

There are three methods available in Python for removing elements in the list.

  • remove()
  • pop()
  • clear()

remove() METHOD

# Removing List elements using remove() example 1

int_list = [400, 200, 600, 100, 150, 200] 
print("\nElement of list int_list at index 0->", int_list[0]) 
print("Element of list int_list at index 1->", int_list[1]) 
print("Element of list int_list at index 2->", int_list[2]) 
print("Element of list int_list at index 3->", int_list[3]) 
print("Element of list int_list at index 4->", int_list[4]) 
print("Element of list int_list at index 5->", int_list[5]) 
print("\nNumber of element in the list int_list ->", len(int_list))

print("Index position of 600 in the list int_list ->", int_list.index(600)) 
print("Element of list int_list at index 2->", int_list[2]) 
int_list.remove(600);
print("After removal of 600 the element of list int_list at index 2->", int_list[2]) 
print("\nNumber of element in the list int_list ->", len(int_list))

REMOVING ELEMENT IN THE LISTS : Output

Element of list int_list at index 0-> 400
Element of list int_list at index 1-> 200
Element of list int_list at index 2-> 600
Element of list int_list at index 3-> 100
Element of list int_list at index 4-> 150
Element of list int_list at index 5-> 200

Number of element in the list int_list -> 6
Index position of 600 in the list int_list -> 2
Element of list int_list at index 2-> 600
After removal of 600 the element of list int_list at index 2-> 100

Number of element in the list int_list -> 5

In the above example, element 600 has been removed from index position 2. After removal of 600 next element 100 is now at index position 2.

# Removing List elements using remove() example 2

str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML'] 
print("\nElement of list str_list at index 0->", str_list[0]) 
print("Element of list str_list at index 1->", str_list[1]) 
print("Element of list str_list at index 2->", str_list[2]) 
print("Element of list str_list at index 3->", str_list[3]) 
print("Element of list str_list at index 4->", str_list[4]) 
print("Element of list str_list at index 5->", str_list[5]) 
print("\nNumber of element in the list str_list ->", len(str_list))

print("Index position of 600 in the list str_list ->", str_list.index('C++')) 
print("Element of list str_list at index 2->", str_list[2]) 
str_list.remove('C++');
print("After removal of C++ the element of list str_list at index 2->", str_list[2]) 
print("\nNumber of element in the list str_list ->", len(str_list))

REMOVING ELEMENT IN THE LISTS : Output

Element of list str_list at index 0-> Python
Element of list str_list at index 1-> Java
Element of list str_list at index 2-> C++
Element of list str_list at index 3-> C
Element of list str_list at index 4-> PHP
Element of list str_list at index 5-> XML

Number of element in the list str_list -> 6
Index position of 600 in the list str_list -> 2
Element of list str_list at index 2-> C++
After removal of C++ the element of list str_list at index 2-> C

Number of element in the list str_list -> 5

In the above example, element C++ has been removed from index position 2. After removal of C++, next element C is now at index position 2.

EXCEPTION in remove() METHOD

If we try to remove any element which doesn’t exists then Python will raise error.

# Exception in removing List elements example 1

int_list = [400, 200, 600, 100, 150, 200] 
print("\nElement of list int_list at index 0->", int_list[0]) 
print("Element of list int_list at index 1->", int_list[1]) 
print("Element of list int_list at index 2->", int_list[2]) 
print("Element of list int_list at index 3->", int_list[3]) 
print("Element of list int_list at index 4->", int_list[4]) 
print("Element of list int_list at index 5->", int_list[5]) 
print("\nNumber of element in the list int_list ->", len(int_list))

int_list.remove(900);
print("\nNumber of element in the list int_list ->", len(int_list))

REMOVING ELEMENT IN THE LISTS : Output

Element of list int_list at index 0-> 400
Element of list int_list at index 1-> 200
Element of list int_list at index 2-> 600
Element of list int_list at index 3-> 100
Element of list int_list at index 4-> 150
Element of list int_list at index 5-> 200

Number of element in the list int_list -> Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.python", line 15, in <module>
    int_list.remove(900);
    ^^^^^^^^^^^^^^^^^^^^
ValueError: list.remove(x): x not in list

In the above example we have tried to remove an element 900 which doesn’t exists in the list. So Python has raised the Value error.

REMOVING ELEMENT IN THE LISTS : Output
REMOVING ELEMENT IN THE LISTS : Output

# Exception in removing List elements example 2

str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML'] 
print("\nElement of list str_list at index 0->", str_list[0]) 
print("Element of list str_list at index 1->", str_list[1]) 
print("Element of list str_list at index 2->", str_list[2]) 
print("Element of list str_list at index 3->", str_list[3]) 
print("Element of list str_list at index 4->", str_list[4]) 
print("Element of list str_list at index 5->", str_list[5]) 
print("\nNumber of element in the list str_list ->", len(str_list))

str_list.remove('Word');
print("\nNumber of element in the list str_list ->", len(str_list))

REMOVING ELEMENT IN THE LISTS : Output

Element of list str_list at index 0-> Python
Element of list str_list at index 1-> Java
Element of list str_list at index 2-> C++
Element of list str_list at index 3-> C
Element of list str_list at index 4-> PHP
Element of list str_list at index 5-> XML

Number of element in the list str_list -> 6
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.python", line 15, in <module>
    str_list.remove('Word');
    ^^^^^^^^^^^^^^^^^^^^^^^
ValueError: list.remove(x): x not in list

In the above example we have tried to remove an element ‘Word’ which doesn’t exists in the list. So Python has raised the Value error.

pop() METHOD

pop() method removes an element from the end.

# Removing List elements using pop() example 1

int_list = [400, 200, 600, 100, 150, 900] 
print("\nElement of list int_list at index 0->", int_list[0]) 
print("Element of list int_list at index 1->", int_list[1]) 
print("Element of list int_list at index 2->", int_list[2]) 
print("Element of list int_list at index 3->", int_list[3]) 
print("Element of list int_list at index 4->", int_list[4]) 
print("Element of list int_list at index 5->", int_list[5]) 
print("\nNumber of element in the list int_list ->", len(int_list))

int_list.pop();
print("Element of list int_list at index 5->", int_list.count(900)) 
print("\nNumber of element in the list int_list ->", len(int_list))

REMOVING ELEMENT IN THE LISTS : Output

Element of list int_list at index 0-> 400
Element of list int_list at index 1-> 200
Element of list int_list at index 2-> 600
Element of list int_list at index 3-> 100
Element of list int_list at index 4-> 150
Element of list int_list at index 5-> 900

Number of element in the list int_list -> 6
Element of list int_list at index 5-> 0

Number of element in the list int_list -> 5

In the above example, we have seen that by using pop() method last element 900 has been removed from the list. So when we use the count() method to get the count of 900 in the list its showing 0. Also the total no of element of the list has been reduced to 5.

# Removing List elements using pop() example 2

str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML'] 
print("\nElement of list str_list at index 0->", str_list[0]) 
print("Element of list str_list at index 1->", str_list[1]) 
print("Element of list str_list at index 2->", str_list[2]) 
print("Element of list str_list at index 3->", str_list[3]) 
print("Element of list str_list at index 4->", str_list[4]) 
print("Element of list str_list at index 5->", str_list[5]) 
print("\nNumber of element in the list str_list ->", len(str_list))

str_list.pop();
print("Element of list str_list at index 5->", str_list.count('XML')) 
print("\nNumber of element in the list str_list ->", len(str_list))

REMOVING ELEMENT IN THE LISTS : Output

Element of list str_list at index 0-> Python
Element of list str_list at index 1-> Java
Element of list str_list at index 2-> C++
Element of list str_list at index 3-> C
Element of list str_list at index 4-> PHP
Element of list str_list at index 5-> XML

Number of element in the list str_list -> 6
Element of list str_list at index 5-> 0

Number of element in the list str_list -> 5

In the above example, we have seen that by using pop() method last element XML has been removed from the list. So when we use the count() method to get the count of XML in the list its showing 0. Also the total no of element of the list has been reduced to 5.

EXCEPTION in pop() METHOD

If we try execute pop() method on an empty list then Python will raise Index error.

# Exception in removing List elements using pop() example 1

int_list = [400]  
print("\nElement of list int_list at index 0->", int_list[0]) 
print("Number of element in the list int_list ->", len(int_list))

int_list.pop();
print("\nElement of list int_list at index 0 ->", int_list.count(400)) 
print("Number of element in the list int_list ->", len(int_list))

int_list.pop();
print("\nElement of list int_list at index 0 ->", int_list.count(400)) 
print("Number of element in the list int_list ->", len(int_list))

REMOVING ELEMENT IN THE LISTS : Output

Element of list int_list at index 0-> 400
Number of element in the list int_list -> 1

Element of list int_list at index 0 -> 0
Number of element in the list int_list -> 0
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.python", line 14, in <module>
    int_list.pop();
    ^^^^^^^^^^^^^^
IndexError: pop from empty list

In the above example, we have taken only one element 400. So, we can see up to first pop() method no error has been returned by Python. After second pop() Python has raised the Index Error due to empty list.

# Exception in removing List elements using pop() example 2

str_list = ['Python'] 
print("\nElement of list str_list at index 0->", str_list[0]) 
print("Number of element in the list str_list ->", len(str_list))

str_list.pop();
print("\nElement of list str_list at index 0 ->", str_list.count('Python')) 
print("Number of element in the list str_list ->", len(str_list))

str_list.pop();
print("\nElement of list str_list at index 0 ->", str_list.count('Python')) 
print("Number of element in the list str_list ->", len(str_list))

REMOVING ELEMENT IN THE LISTS : Output

Element of list str_list at index 0-> Python
Number of element in the list str_list -> 1

Element of list str_list at index 0 -> 0
Number of element in the list str_list -> 0
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.python", line 14, in <module>
    str_list.pop();
    ^^^^^^^^^^^^^^
IndexError: pop from empty list

In the above example, we have taken only one element ‘Python‘. So, we can see up to first pop() method no error has been returned by python. After second pop() python has raised the Index Error due to empty list.

clear() METHOD

clear() method helps us to clear the complete list at a time.

# Removing all the list elements at a time using clear() example 1

int_list = [400, 200, 600, 100, 150, 200] 
print("\nElement of list int_list at index 0->", int_list[0]) 
print("Element of list int_list at index 1->", int_list[1]) 
print("Element of list int_list at index 2->", int_list[2]) 
print("Element of list int_list at index 3->", int_list[3]) 
print("Element of list int_list at index 4->", int_list[4]) 
print("Element of list int_list at index 5->", int_list[5]) 
print("\nNumber of element in the list int_list ->", len(int_list))

int_list.clear()
print("\nNumber of element in the list int_list after using clear() -> ", len(int_list))

REMOVING ELEMENT IN THE LISTS : Output

Element of list int_list at index 0-> 400
Element of list int_list at index 1-> 200
Element of list int_list at index 2-> 600
Element of list int_list at index 3-> 100
Element of list int_list at index 4-> 150
Element of list int_list at index 5-> 200

Number of element in the list int_list -> 6

Number of element in the list int_list after using clear() ->  0

In the above example, we can see that after using the clear() method all the element has been removed. That’s why the count of element has been reduced to 0.

# Removing all the list elements at a time using clear() example 2

str_list = ['Python', 'Java', 'C++', 'C', 'PHP', 'XML'] 
print("\nElement of list str_list at index 0->", str_list[0]) 
print("Element of list str_list at index 1->", str_list[1]) 
print("Element of list str_list at index 2->", str_list[2]) 
print("Element of list str_list at index 3->", str_list[3]) 
print("Element of list str_list at index 4->", str_list[4]) 
print("Element of list str_list at index 5->", str_list[5]) 
print("\nNumber of element in the list str_list ->", len(str_list))

str_list.clear()
print("\nNumber of element in the list str_list after using clear()->", len(str_list))

REMOVING ELEMENT IN THE LISTS : Output

Element of list str_list at index 0-> Python
Element of list str_list at index 1-> Java
Element of list str_list at index 2-> C++
Element of list str_list at index 3-> C
Element of list str_list at index 4-> PHP
Element of list str_list at index 5-> XML

Number of element in the list str_list -> 6

Number of element in the list str_list after using clear()-> 0

RELATED TOPICS:

Leave a Comment

Your email address will not be published. Required fields are marked *