Skip to content
Programming Problems · Q5

Q.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.

Odisha ChseTextbookSubjective· 3mImportance★★★★★est
83% · 25/30 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Walk the original list and copy each element into a new list only if it isn't already there

(if item not in unique). This keeps the first occurrence of every element and preserves order —

then assign the result back so the original list is "modified".

The idea

A duplicate is any element we have seen before while scanning. So the test

item not in unique — membership in the elements collected so far — is exactly the

"first time I'm seeing this" check.

Program

# Remove duplicate elements from a list
n = int(input("How many elements? "))
lst = []
for i in range(n):
    lst.append(input("Enter element " + str(i + 1) + ": "))

unique = []
for item in lst:
    if item not in unique:     # first occurrence -> keep it
        unique.append(item)    # later occurrences fail the test and are skipped

lst = unique                   # modify: lst now has no duplicates
print("List without duplicates:", lst)

Sample run

How many elements? 7
Enter element 1: red
Enter element 2: blue
Enter element 3: red
Enter element 4: green
Enter element 5: blue
Enter element 6: red
Enter element 7: green
List without duplicates: ['red', 'blue', 'green']

Why not just list(set(lst))?

set() also removes duplicates, but a set has no guaranteed order, so

list(set(lst)) can shuffle the elements. The loop above keeps the original …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.