List Manipulation in Python
1. Introduction to Lists
- A list is an ordered collection of elements enclosed within square brackets [ ].
- Elements in a list can be of different data types (integers, strings, floats, etc.).
- Lists are mutable, meaning their elements can be changed after creation.
Example:
numbers = [10, 20, 30, 40]
mixed = [10, "Hello", 3.5, True]
2. Indexing in Lists
- Each element in a list has a position number called an index.
- Indexing starts from 0 for the first element.
- Negative indexing starts from
-1for the last element.
Example:
fruits = ['apple', 'banana', 'cherry', 'date']
print(fruits[0]) # apple
print(fruits[-1]) # date
3. List Operations
(a) Concatenation (+)
Used to join two or more lists.
a = [1, 2]
b = [3, 4]
c = a + b
print(c) # [1, 2, 3, 4]
(b) Repetition (*)
Repeats elements of a list.
x = [10, 20]
print(x * 3) # [10, 20, 10, 20, 10, 20]
(c) Membership (in / not in)
Checks if an element exists in a list.
names = ['Raj', 'Simran', 'Amit']
print('Raj' in names) # True
print('Rahul' not in names) # True
(d) Slicing
Used to extract parts of a list.
nums = [10, 20, 30, 40, 50]
print(nums[1:4]) # [20, 30, 40]
print(nums[:3]) # [10, 20, 30]
print(nums[::2]) # [10, 30, 50]
4. Traversing a List Using Loops
Using for loop
fruits = ['apple', 'banana', 'cherry']
for item in fruits:
print(item)
Using while loop
i = 0
while i < len(fruits):
print(fruits[i])
i += 1
5. Built-in Functions and Methods
| Function/Method | Description | Example |
|---|---|---|
len(list) | Returns number of elements | len([1,2,3]) → 3 |
list(seq) | Converts sequence into list | list('abc') → ['a','b','c'] |
append(x) | Adds an element at the end | nums.append(5) |
extend(list2) | Adds all elements of another list | nums.extend([6,7]) |
insert(pos, val) | Inserts element at given position | nums.insert(1, 50) |
count(x) | Counts occurrences of an element | nums.count(10) |
index(x) | Returns index of first occurrence | nums.index(20) |
remove(x) | Removes first occurrence of element | nums.remove(20) |
pop([pos]) | Removes and returns element | nums.pop() or nums.pop(2) |
reverse() | Reverses list elements | nums.reverse() |
sort() | Sorts list in ascending order | nums.sort() |
sorted(list) | Returns a sorted copy (original unchanged) | sorted(nums) |
min(list) | Returns smallest element | min(nums) |
max(list) | Returns largest element | max(nums) |
sum(list) | Returns sum of numeric elements | sum(nums) |
6. Nested Lists
- List within a list is known as nested list.
- A nested list is a list that contains other lists as elements.
Example:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[0]) # [1, 2, 3]
print(matrix[1][2]) # 6
7. Suggested Programs
(a) Finding Maximum, Minimum, and Mean
nums = [10, 20, 30, 40, 50]
print("Maximum:", max(nums))
print("Minimum:", min(nums))
mean = sum(nums) / len(nums)
print("Mean:", mean)
(b) Linear Search
Search for a number in a list.
nums = [12, 45, 23, 39, 45, 10]
x = int(input("Enter number to search: "))
found = False
for i in range(len(nums)):
if nums[i] == x:
print("Found at position", i)
found = True
break
if not found:
print("Not found")
(c) Counting Frequency of Elements
nums = [2, 3, 4, 2, 2, 5, 3]
unique = []
for x in nums:
if x not in unique:
print(x, "occurs", nums.count(x), "times")
unique.append(x)
8. Key Points to Remember
- Lists are mutable, ordered, and can hold heterogeneous data.
- Indexing starts from 0; negative indexing starts from -1.
- Common list functions make data manipulation easy.
- Nested lists can represent matrices or tables.
- Traversing helps in performing operations like searching, counting, or computing values.
Below are 30 MCQs for Class XI Computer Science based on Lists and List Operations, including output-based questions.
➡️ Answers are provided after the questions.
MCQs – Lists in Python
1. What is the output of the following code?
lst = [10, 20, 30]
print(lst[1])
A. 10
B. 20
C. 30
D. IndexError
Answer
B
2. Which operator is used to concatenate two lists in Python?
A. +
B. *
C. &
D. %
3. What is the output of the following code?
lst = [1, 2, 3]
print(lst * 2)
A. [1, 2, 3, 1, 2, 3]
B. [1, 2, 3, 4, 5, 6]
C. [2, 4, 6]
D. [1, 1, 2, 2, 3, 3]
4. Which of the following methods is used to add an element to the end of a list?
A. append()
B. insert()
C. extend()
D. add()
5. The extend() method in Python is used to:
A. Add a single element to a list
B. Add elements from another iterable to a list
C. Insert an element at a specific position
D. Remove elements from a list
6. What is the output of the following code?
lst = [1, 2, 3, 4]
lst.append([5, 6])
print(lst)
A. [1, 2, 3, 4, 5, 6]
B. [1, 2, 3, 4]
C. [1, 2, 3, 4, [5, 6]]
D. Error
7. What will be the output of this code?
lst = [5, 10, 15, 20]
lst.insert(2, 12)
print(lst)
A. [5, 10, 12, 15, 20]
B. [5, 10, 15, 12, 20]
C. [12, 5, 10, 15, 20]
D. Error
8. Which of the following methods is used to find the frequency of an element in a list?
A. index()
B. count()
C. find()
D. frequency()
9. What is the output of the following code?
lst = [2, 4, 6, 8]
print(lst.index(6))
A. 2
B. 3
C. 1
D. Error
10. What is the output of the following code?
lst = [10, 20, 30, 40, 50]
lst.remove(30)
print(lst)
A. [10, 20, 30, 40, 50]
B. [10, 20, 40, 50]
C. [20, 30, 40, 50]
D. Error
11. What is the output of the following code?
lst = [1, 2, 3, 4]
popped_value = lst.pop()
print(popped_value)
A. 1
B. 4
C. 3
D. Error
12. The reverse() method in Python is used to:
A. Sort the list in ascending order
B. Reverse the order of elements in the list
C. Reverse the order of elements without changing the list
D. None of the above
13. Which function is used to sort a list in Python?
A. sort()
B. order()
C. ascending()
D. arrange()
14. What is the output of the following code?
lst = [3, 1, 4, 2]
lst.sort()
print(lst)
A. [1, 2, 3, 4]
B. [3, 1, 4, 2]
C. [4, 3, 2, 1]
D. Error
15. Which function returns a sorted copy of a list?
A. sorted()
B. sort()
C. order()
D. arrange()
16. What will be the output of the following code?
lst = [2, 4, 6, 8, 10]
print(min(lst))
A. 10
B. 2
C. 8
D. Error
17. What is the output of this code?
lst = [10, 20, 30, 40, 50]
print(max(lst))
A. 10
B. 50
C. 40
D. Error
18. Which method is used to sum all elements in a list?
A. add()
B. total()
C. sum()
D. accumulate()
19. What is the output of this code?
lst = [1, 2, 3]
lst.extend([4, 5])
print(lst)
A. [1, 2, 3]
B. [4, 5, 1, 2, 3]
C. [1, 2, 3, 4, 5]
D. Error
20. What is the output of the following code?
lst = [10, 20, 30, 40]
lst.sort(reverse=True)
print(lst)
A. [40, 30, 20, 10]
B. [10, 20, 30, 40]
C. [20, 30, 10, 40]
D. Error
21. Which of the following is a method to remove the last item from a list?
A. remove()
B. pop()
C. delete()
D. discard()
22. What is the result of slicing the list lst = [1, 2, 3, 4, 5] with lst[1:4]?
A. [1, 2, 3]
B. [2, 3, 4]
C. [1, 2, 3, 4]
D. [4, 5]
23. What does the insert() method do?
A. Adds an element at the beginning of the list
B. Inserts an element at a specific index
C. Appends an element to the end of the list
D. Removes an element from the list
24. What is the output of the following code?
lst = [3, 2, 1, 4, 5]
lst.sort(reverse=True)
print(lst)
A. [1, 2, 3, 4, 5]
B. [5, 4, 3, 2, 1]
C. [3, 2, 1, 4, 5]
D. Error
25. What is the result of the following operation?
lst = [1, 2, 3, 4]
print(len(lst))
A. 4
B. 3
C. 5
D. Error
26. How can you check if an element exists in a list?
A. using the in operator
B. using the exist() function
C. using the find() method
D. using the search() method
27. What is the result of this operation?
lst = [1, 2, 3, 4]
lst.pop(2)
print(lst)
A. [1, 2, 3]
B. [1, 2, 4]
C. [1, 3, 4]
D. [2, 3, 4]
28. Which of the following is a nested list?
A. [1, 2, 3]
B. [[1, 2], [3, 4]]
C. [5, 6, 7]
D. [8, 9]
29. How would you find the maximum value in a list of numbers?
A. max(list)
B. list.max()
C. find_max(list)
D. get_max(list)
30. What is the output of the following code?
lst = [5, 10, 5, 20, 5]
print(lst.count(5))
``
`
A. 3
B. 2
C. 5
D. 4
✅ ANSWER KEY
1-B
2-A
3-A
4-A
5-B
6-C
7-A
8-B
9-A
10-B
11-B
12-B
13-A
14-A
15-A
16-B
17-B
18-C
19-C
20-A
21-B
22-B
23-B
24-B
25-A
26-A
27-B
28-B
29-A
30-A