Pda Lab 2
Pda Lab 2
Which of the above operations can be performed directly? Which of the above operations cannot be
performed directly on a Tuple and why? Update and remove a specific item from the tuple by
converting it into a list.
Create a list named "Subjects" and insert 10 subjects into it using a for loop
Subjects = []
print(Elective_Subjects)
Extend list “Subject” by another list “Elective Subjects”.
# Extend the student subjects list with the elective subjects list
Subjects.extend(Elective_Subjects)
print(Subjects)
Append 3 duplicate subjects into “Subject” list.
Subjects.extend(duplicate_subjects)
print(Subjects)
Find the index of first occurrence of that duplicate value and then remove all
the occurrences of that specific subject through loop.
# Identify and remove duplicates, keeping the first occurrence
seen = set()
result = []
for subject in Subjects:
if subject not in seen:
seen.add(subject)
result.append(subject)
elif subject in duplicate_subjects:
duplicate_subjects.remove(subject)
# Operations on tuples
tuple_subjects = ("Math", "Science", "English", "History", "Geography")
# Convert tuple to list to update and remove specific items
list_subjects = list(tuple_subjects)
list_subjects[2] = "Physics" # Update specific item
list_subjects.remove("History") # Remove specific item
tuple_subjects = tuple(list_subjects)