Python List Methods (Part 3) | remove(), pop(), reverse(), sort(), sorted(), min(), max(), sum() | CBSE Class 11 Computer Science
Class 11 · Computer Science
Python List Methods (Part 3)
Python provides several methods and functions to remove elements, reverse lists, sort data, and perform mathematical operations. These methods are widely used while processing numerical data and solving real-world programming problems.
Learning Objectives
- Remove elements from a list.
- Reverse and sort a list.
- Understand the difference between
sort()andsorted(). - Find minimum, maximum, and sum of list elements.
1. remove() Method
The remove() method removes the first occurrence of the specified element from a list.
Syntax
list.remove(element)
Example
numbers=[10,20,30,20,40]
numbers.remove(20)
print(numbers)
Output
[10,30,20,40]
Explanation: Only the first occurrence of 20 is removed.
Important Note
If the specified element is not present in the list, Python raises a ValueError.
numbers=[10,20,30]
numbers.remove(50)
Output
ValueError
2. pop() Method
The pop() method removes and returns an element from the specified index. If no index is provided, it removes the last element.
Syntax
list.pop(index)
Example 1
numbers=[10,20,30,40]
numbers.pop()
print(numbers)
Output
[10,20,30]
Example 2
numbers=[10,20,30,40]
numbers.pop(1)
print(numbers)
Output
[10,30,40]
Difference between remove() and pop()
| remove() | pop() |
|---|---|
| Removes by value. | Removes by index. |
| Returns nothing. | Returns the removed element. |
| Raises ValueError if value is absent. | Raises IndexError for an invalid index. |
3. reverse() Method
The reverse() method reverses the order of elements in the original list.
Syntax
list.reverse()
Example
numbers=[10,20,30,40]
numbers.reverse()
print(numbers)
Output
[40,30,20,10]
4. sort() Method
The sort() method arranges the elements of the original list in ascending order by default.
Syntax
list.sort()
Example
numbers=[40,10,30,20]
numbers.sort()
print(numbers)
Output
[10,20,30,40]
Descending Order
numbers=[40,10,30,20]
numbers.sort(reverse=True)
print(numbers)
Output
[40,30,20,10]
5. sorted() Function
The sorted() function returns a new sorted list without modifying the original list.
Syntax
sorted(list)
Example
numbers=[40,10,30,20]
newlist=sorted(numbers)
print(numbers)
print(newlist)
Output
[40,10,30,20]
[10,20,30,40]
Difference between sort() and sorted()
| sort() | sorted() |
|---|---|
| Modifies the original list. | Returns a new sorted list. |
| Works only with lists. | Works with many iterable objects. |
| Returns None. | Returns a sorted list. |
6. min() Function
The min() function returns the smallest element from a list.
Syntax
min(list)
Example
marks=[85,90,72,96]
print(min(marks))
Output
72
7. max() Function
The max() function returns the largest element from a list.
Syntax
max(list)
Example
marks=[85,90,72,96]
print(max(marks))
Output
96
8. sum() Function
The sum() function returns the sum of all numeric elements in a list.
Syntax
sum(list)
Example
marks=[85,90,72,96]
print(sum(marks))
Output
343
Summary Table
| Function / Method | Purpose |
|---|---|
remove() |
Removes the first matching value. |
pop() |
Removes an element by index and returns it. |
reverse() |
Reverses the original list. |
sort() |
Sorts the original list. |
sorted() |
Returns a new sorted list. |
min() |
Returns the smallest element. |
max() |
Returns the largest element. |
sum() |
Returns the total of all numeric elements. |
Real-Life Applications
- Finding the highest and lowest marks of students.
- Calculating total sales.
- Sorting examination results.
- Maintaining ranking lists.
- Removing cancelled orders from a list.
- Displaying records in alphabetical order.
Common Programming Mistakes
- Confusing
sort()withsorted(). - Using
remove()for an element that does not exist. - Calling
pop()with an invalid index. - Applying
sum()to a list containing strings. - Expecting
sort()to return a new list.
CBSE Important Programs
- Remove an element from a list.
- Remove the last element using
pop(). - Reverse a list.
- Sort a list in ascending order.
- Sort a list in descending order.
- Find the minimum value.
- Find the maximum value.
- Calculate the sum of list elements.
- Display the removed element using
pop(). - Create a sorted copy of a list using
sorted().
Viva Questions
- What is the difference between
remove()andpop()? - Which method removes an element by value?
- Which method removes an element by index?
- Differentiate between
sort()andsorted(). - Which function returns the smallest element?
- Which function returns the largest element?
- What is the purpose of the
sum()function? - Can
sort()arrange elements in descending order? - What happens if
remove()cannot find the specified value? - What happens if
pop()is used with an invalid index?
Exam Tips
- Use
remove()to delete by value. - Use
pop()to delete by index. - Remember that
sort()modifies the original list. - Use
sorted()when the original list must remain unchanged. min(),max(), andsum()work with numeric lists.
Quick Revision
remove()removes the first matching value.pop()removes by index and returns the removed element.reverse()reverses the original list.sort()sorts the original list.sorted()returns a new sorted list.min()returns the smallest value.max()returns the largest value.sum()returns the total of numeric elements.