Write a program to read a list of elements. Modify this list so that it does not contain any duplicate elements, i.e., all elements occurring multiple times in the list should appear only once.
Program to remove duplicate elements from a list
Create an empty list
myList = []
Read number of elements
n = int(input("Enter the number of elements: "))
Read elements into the list
for i in range(n): element = int(input("Enter element: ")) myList.append(element)
print("Original list:", myList)
Create a new list without duplicates
newList = [] for element in myList: if element not in newList: newList.append(element)
Display the modified list
print("List after removing duplicates:", newList)
Explanation
This question tests the student's understanding of list manipulation and membership testing. The program reads elements from the user, stores them in a list, and then creates a new list containing only unique elements. The logic uses a membership operator (in) to check if an element already exists in the new list before appending it. This ensures that each element appears only once in the final list. The approach demonstrates fundamental list operations like append() and iteration through list elements using a for loop, which are core concepts covered in the NCERT textbook chapter on list manipulation.
Solution Steps
Step 1: Create an empty list and read n elements from the user using a for loop and append() method.
Step 2: Display the original list to show the input with duplicates.
Step 3: Create an empty newList to store unique elements.
Step 4: Iterate through each element in the original list and check if it exists in newList using 'not in' operator.
Step 5: If element is not in newList, append it. This ensures only unique elements are added.
Step 6: Display the final list without duplicates.