🌟 Diplomatech Academy 🌟
Manual Solutions and outputs
Python Programming Course Code: 314004
-Mohade Sir©
Practical No. 8: Write python program to use built-in functions/methods on
list:
cmp, len, max, list, append, count, extend, insert, pop, remove, etc.
ir
eS
IX Conclusion:
Ans.
ad
Conclusion
We have successfully demonstrated the use of various built-in list functions in
Python. These functions help in performing operations like comparing lists, finding
length, maximum value, modifying lists by adding, removing, or updating elements
oh
efficiently. Understanding these functions makes list manipulation easier and more
effective in Python programming.
M
X Practical related questions
1) Write syntax for a method to reverse a list.
Ans.
Syntax to Reverse a List in Python
Using reverse() (Modifies Original List)
list_name.reverse()
1.
Using Slicing ([::-1]) (Returns New Reversed List)
reversed_list = list_name[::-1]
ir
2.
eS
2) Describe various list functions.
ans/
ad
Various List Functions in Python
1. append(item) – Adds an item to the end of the list.
2. insert(index, item) – Inserts an item at a specific index.
oh
3. remove(item) – Removes the first occurrence of an item.
4. pop(index) – Removes and returns an item (default: last).
5. sort() / sorted(lst) – Sorts the list (modifies / returns new sorted
list).
M
6. reverse() – Reverses the list order.
7. index(item) – Returns the index of the first occurrence.
8. count(item) – Counts occurrences of an item.
3) Describe the use of pop operator in list.
Ans.
Use of pop() Operator in Lists
The pop() function in Python removes and returns an element from a list at a
specified index. If no index is given, it removes the last element by default.
Syntax:
list_name.pop(index) # Removes element at the given index
ir
list_name.pop() # Removes the last element if index is not provided
eS
Example:
numbers = [10, 20, 30, 40]
removed_item = numbers.pop(1) # Removes 20 (index 1)
ad
print(numbers) # Output: [10, 30, 40]
print("Removed Item:", removed_item) # Output: 20
oh
Key Points:
✅ Modifies the original list
✅ Returns the removed element
✅ Raises IndexError if the list is empty or index is out of range
M
4) Describe the use extend & pop method in list.
Ans.
Use of extend() and pop() Methods in Lists
1. extend() Method
● Purpose:Adds multiple elements from another iterable (list, tuple, set) to the
end of a list.
Syntax:
list1.extend(iterable)
●
Example:
ir
list1 = [1, 2, 3]
list1.extend([4, 5, 6])
●
eS
print(list1) # Output: [1, 2, 3, 4, 5, 6]
2. pop() Method
ad
● Purpose: Removes and returns an element from a list (default: last item).
Syntax:
list_name.pop(index) # Removes element at the given index
oh
list_name.pop() # Removes the last element if index is not provided
●
Example:
M
numbers = [10, 20, 30, 40]
removed_item = numbers.pop(1) # Removes 20 (index 1)
print(numbers) # Output: [10, 30, 40]
print("Removed Item:", removed_item) # Output: 20
●
Key Points:
✅ extend() expands a list with multiple elements.
✅ pop() removes and returns an element from a list.
5) Write a Python program to find common items from two lists.
Ans.
ir
Python Program to Find Common Items from Two Lists
# Define two lists
eS
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
ad
# Find common elements using set intersection
common_items = list(set(list1) & set(list2))
oh
# Display the result
print("Common items:", common_items)
M
Output:
Common items: [4, 5]
6) Write a Python program to sort a list.
Ans.
Python Program to Sort a List
Method 1: Using sort() (Modifies Original List)
numbers = [5, 2, 9, 1, 7]
numbers.sort() # Sorts the list in ascending order
ir
print("Sorted List:", numbers)
eS
Output:
Sorted List: [1, 2, 5, 7, 9]
ad
oh
M