Python List Functions & Methods (Part 2) | len(), list(), append(), extend(), insert(), count(), index() | CBSE Class 11 Computer Science
Class 11 · Computer Science
Python List Functions & Methods (Part 2)
Python provides several built-in functions and methods to create, modify, search, and manage lists efficiently. These functions simplify programming by reducing the amount of code required to perform common operations on lists.
In this article, we will study the following functions and methods:
- len()
- list()
- append()
- extend()
- insert()
- count()
- index()
Learning Objectives
- Find the length of a list.
- Create lists using the
list()function. - Add elements to a list.
- Merge two lists.
- Insert elements at a specific position.
- Count and search list elements.
1. len() Function
The len() function returns the total number of elements present in a list.
Syntax
len(list)
Example 1
numbers=[10,20,30,40]
print(len(numbers))
Output
4
Example 2
students=["Aman","Riya","Rahul","Priya","Neha"]
print(len(students))
Output
5
Applications
- Finding the number of students.
- Counting products in an inventory.
- Validating list size before processing.
2. list() Function
The list() function creates a new list from an iterable object such as a string, tuple, or range.
Syntax
list(iterable)
Example 1
text="Python"
letters=list(text)
print(letters)
Output
['P','y','t','h','o','n']
Example 2
numbers=list(range(1,6))
print(numbers)
Output
[1,2,3,4,5]
3. append() Method
The append() method adds a single element to the end of a list.
Syntax
list.append(element)
Example 1
numbers=[10,20,30]
numbers.append(40)
print(numbers)
Output
[10,20,30,40]
Example 2
subjects=["English","Physics"]
subjects.append("Chemistry")
print(subjects)
Output
['English','Physics','Chemistry']
Important Note
The append() method adds only one element. If a list is appended, the entire list becomes a single nested element.
list1=[1,2]
list1.append([3,4])
print(list1)
Output
[1,2,[3,4]]
4. extend() Method
The extend() method adds all elements of another iterable to the end of the list.
Syntax
list.extend(iterable)
Example
list1=[1,2]
list2=[3,4]
list1.extend(list2)
print(list1)
Output
[1,2,3,4]
Difference between append() and extend()
| append() | extend() |
|---|---|
| Adds one element. | Adds all elements. |
| Creates a nested list if another list is appended. | Merges the elements of both lists. |
5. insert() Method
The insert() method inserts an element at a specified index.
Syntax
list.insert(index,element)
Example
numbers=[10,20,40]
numbers.insert(2,30)
print(numbers)
Output
[10,20,30,40]
6. count() Method
The count() method returns the number of times a specified element appears in a list.
Syntax
list.count(element)
Example
numbers=[10,20,10,30,10]
print(numbers.count(10))
Output
3
Applications
- Counting attendance status.
- Finding repeated values.
- Frequency analysis.
7. index() Method
The index() method returns the index of the first occurrence of the specified element.
Syntax
list.index(element)
Example
numbers=[50,40,20,30]
print(numbers.index(20))
Output
2
Important Note
If the specified element is not found, the index() method raises a ValueError.
numbers=[10,20,30]
print(numbers.index(50))
Output
ValueError
Summary Table
| Function / Method | Purpose |
|---|---|
len() |
Returns the number of elements. |
list() |
Creates a list from an iterable. |
append() |
Adds one element at the end. |
extend() |
Adds all elements of another iterable. |
insert() |
Inserts an element at a specified position. |
count() |
Counts occurrences of an element. |
index() |
Returns the index of the first occurrence. |
Common Programming Mistakes
- Confusing
append()withextend(). - Using
index()for elements that do not exist. - Forgetting that indexing starts from 0.
- Using
insert()with an incorrect index. - Expecting
append()to merge two lists.
CBSE Important Programs
- Create a list using the
list()function. - Add an element using
append(). - Merge two lists using
extend(). - Insert an element at a specified position.
- Count the frequency of an element.
- Find the position of an element.
- Display the length of a list.
- Create a list from a string.
- Create a list using
range(). - Find the first occurrence of a value.
Viva Questions
- What is the purpose of the
len()function? - How does the
list()function work? - Differentiate between
append()andextend(). - What is the purpose of the
insert()method? - Which method counts the occurrences of an element?
- Which method returns the position of an element?
- What happens if
index()cannot find an element? - Can
append()add multiple elements? - Can
extend()accept a tuple? - Give two applications of the
count()method.
Exam Tips
- Remember that
append()adds only one element. - Use
extend()to merge lists. count()returns the frequency of an element.index()returns the first occurrence only.- Practice the difference between
append()andextend().
Quick Revision
len()returns the list size.list()creates a list from an iterable.append()adds one element.extend()merges iterable elements.insert()inserts at a specific index.count()counts occurrences.index()returns the first matching index.