HomePythonPython Program to Remove Duplicates from a List

Python Program to Remove Duplicates from a List

This Python program removes duplicates from a list by converting the list to a set, which automatically removes duplicate elements, and then converting the set back to a list.

In Python, a list is a collection of elements that can contain duplicate values. Removing duplicates from a list involves eliminating repeated elements, so that each element appears only once in the modified list. This can be useful for various tasks like data cleaning and ensuring uniqueness in a dataset.

Problem statement

You are given a list of elements. Your task is to write a Python program to remove duplicates from the list while preserving the original order of elements. Create a function that takes the input list and returns a new list with duplicates removed.

Python Program to Remove Duplicates from a List

def remove_duplicates(input_list):
    output_list = []
    for item in input_list:
        if item not in output_list:
            output_list.append(item)
    return output_list

# Example usage:
my_list = [1, 2, 2, 3, 4, 4, 5]
result = remove_duplicates(my_list)
print(result)  # Output: [1, 2, 3, 4, 5]

How it works

To understand how the program works, let’s break down the Python program step by step:

Code:

def remove_duplicates(input_list):
output_list = [] # Create an empty list to store unique elements
for item in input_list:
if item not in output_list:
output_list.append(item)
return output_list

  1. We define a function called remove_duplicates that takes one argument, input_list, which is the list from which we want to remove duplicates.
  2. We create an empty list called output_list to store the unique elements. This will be the list where we build the result.
  3. We use a for loop to iterate through each item in the input_list. This loop examines each element one by one.
  4. For each element in the input_list, we check whether it’s already in the output_list. We do this using the if item not in output_list condition. This condition checks if the current item is not already present in the output_list.
  5. If the condition is met (i.e., the item is not in output_list), we append it to the output_list using output_list.append(item). This ensures that only unique elements get added to the output_list.
  6. We continue this process for all elements in the input_list, and the output_list gradually accumulates unique elements in the order they appear in the input_list.
  7. Finally, we return the output_list, which now contains all the unique elements from the input_list while preserving their original order.

Here’s how the example usage works:

Code:

my_list = [1, 2, 2, 3, 4, 4, 5]
result = remove_duplicates(my_list)
print(result) # Output: [1, 2, 3, 4, 5]

  • The my_list contains duplicates: [1, 2, 2, 3, 4, 4, 5].
  • When we call remove_duplicates(my_list), it processes each element in the order it appears.
  • It creates a new list output_list and adds elements to it only if they are not already present.
  • After processing all elements, it returns the output_list, which contains unique elements in the original order: [1, 2, 3, 4, 5].

This is how the program works to remove duplicates from a list while preserving the original order of elements.

Input/Output

Python Program to Remove Duplicates from a List

Leave A Reply

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

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...