1. Introduction
Removing duplicates from a list is a common task in data cleaning and preparation. It is important for ensuring that the data set contains only unique elements, which can be necessary for accurate statistical analysis or when preparing data for machine learning algorithms.
Definition
A duplicate in a list is an item that appears more than once. Removing duplicates involves creating a new list that contains each item only once, even if it appears multiple times in the original list.
2. Program Steps
1. Start with the original list that contains duplicate elements.
2. Create a new list to store the unique elements.
3. Iterate over the original list and add each item to the new list only if it is not already present.
4. Print the list without duplicates.
3. Code Program
# Original list with duplicates
original_list = [1, 2, 3, 1, 2, 4, 5, 3, 6, 2, 5]
# New list to hold the unique elements
unique_list = []
# Loop through the original list
for item in original_list:
# Add each item to the new list only if it is not already present
if item not in unique_list:
unique_list.append(item)
# Print the list without duplicates
print(f"List without duplicates: {unique_list}")
Output:
List without duplicates: [1, 2, 3, 4, 5, 6]
Explanation:
1. original_list contains a list of integers where some, like 1 and 2, appear more than once.
2. unique_list is initialized as an empty list that will store the non-duplicate items.
3. The for loop iterates through each item in original_list.
4. The if condition checks if item is not in unique_list. If it isn't, item is added to unique_list using the append() method.
5. After the loop completes, unique_list contains each element from original_list but only once, no matter how many times it appeared in original_list.
6. The print statement outputs the cleaned unique_list, which does not have duplicates and contains [1, 2, 3, 4, 5, 6].
Comments
Post a Comment
Leave Comment