A) Write a Python program to accept n numbers in a list and remove duplicates.

Aim:
To write a Python program that accepts n numbers from the user, stores them in a list, and removes duplicate elements.

Algorithm:

  1. Start the program.
  2. Accept the number of elements (n) from the user.
  3. Create an empty list.
  4. Use a loop to input n numbers and append them to the list.
  5. Convert the list to a set to remove duplicate elements.
  6. Convert the set back to a list.
  7. Display the list without duplicates.
  8. Stop the program.

Program:

n = int(input("Enter the number of elements: "))numbers = []for i in range(n):
num = int(input("Enter number: "))
numbers.append(num)print("Original List:", numbers)# Remove duplicates
unique_numbers = list(set(numbers))print("List after removing duplicates:", unique_numbers)

Sample Output:

Enter the number of elements: 6
Enter number: 2
Enter number: 5
Enter number: 2
Enter number: 7
Enter number: 5
Enter number: 9Original List: [2, 5, 2, 7, 5, 9]
List after removing duplicates: [2, 5, 7, 9]

Oral Questions:

  1. What is a list in Python?
  2. How do you create a list in Python?
  3. What is the use of the append() function in a list?
  4. What is a duplicate element in a list?
  5. How does the set() function remove duplicates?
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top