Introduction
Removing duplicates from a list is a common task in data processing and analysis. Duplicates can occur when data is collected from various sources or when data entries are repeated. This tutorial will guide you through creating a Python program that removes duplicates from a given list, leaving only unique elements.
Example:
-
Input:
[1, 2, 2, 3, 4, 4, 5] -
Output:
[1, 2, 3, 4, 5] -
Input:
[10, 20, 20, 30, 40, 40, 50] -
Output:
[10, 20, 30, 40, 50]
Problem Statement
Create a Python program that:
- Takes a list of numbers as input.
- Removes any duplicate elements in the list.
- Displays the list with duplicates removed.
Solution Steps
- Take Input from the User: Use the
input()function to get a list of numbers from the user. - Convert Input to a List of Integers: Convert the input string to a list of integers.
- Remove Duplicates: Convert the list to a set to automatically remove duplicates, then convert it back to a list.
- Display the List Without Duplicates: Use the
print()function to display the list with duplicates removed.
Python Program
# Python Program to Remove Duplicates from a List
# Author: https://www.rameshfadatare.com/
# Step 1: Take input from the user
input_list = input("Enter a list of numbers separated by spaces: ")
# Step 2: Convert the input string to a list of integers
numbers = list(map(int, input_list.split()))
# Step 3: Remove duplicates by converting the list to a set, then back to a list
unique_numbers = list(set(numbers))
# Step 4: Display the list without duplicates
print(f"The list without duplicates is: {unique_numbers}")
Explanation
Step 1: Take Input from the User
- The
input()function prompts the user to enter a list of numbers, separated by spaces. The input is stored as a string in the variableinput_list.
Step 2: Convert Input to a List of Integers
- The
split()method is used to split the input string into individual components, andmap(int, ...)is used to convert these components into integers. Thelist()function then creates a list of these integers.
Step 3: Remove Duplicates
- The list is converted to a
setusing theset()function, which automatically removes any duplicate elements. Thesetis then converted back to alistto maintain the list format.
Step 4: Display the List Without Duplicates
- The
print()function is used to display the list with duplicates removed.
Output Example
Example 1:
Enter a list of numbers separated by spaces: 1 2 2 3 4 4 5
The list without duplicates is: [1, 2, 3, 4, 5]
Example 2:
Enter a list of numbers separated by spaces: 10 20 20 30 40 40 50
The list without duplicates is: [10, 20, 30, 40, 50]
Example 3:
Enter a list of numbers separated by spaces: 5 10 5 15 20 20
The list without duplicates is: [20, 10, 5, 15]
(Note: The order of elements in the output may vary because sets do not maintain order.)
Conclusion
This Python program demonstrates how to remove duplicates from a list using a set, which is an efficient and straightforward method. Understanding how to handle duplicates is important in data processing and helps ensure that your data is clean and accurate.