[go: up one dir, main page]

0% found this document useful (0 votes)
0 views2 pages

Assignment 26

This document explains how to remove duplicates from a list in Python using sets, which inherently filter out duplicate elements due to their unique nature. The process involves converting the list to a set and then back to a list, maintaining a time complexity close to O(n). It also notes that while this method is efficient, it does not preserve the original order of elements, which may require a different approach if order is important.

Uploaded by

Yash Nagapure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

Assignment 26

This document explains how to remove duplicates from a list in Python using sets, which inherently filter out duplicate elements due to their unique nature. The process involves converting the list to a set and then back to a list, maintaining a time complexity close to O(n). It also notes that while this method is efficient, it does not preserve the original order of elements, which may require a different approach if order is important.

Uploaded by

Yash Nagapure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Assignment 26: Removing Duplicates

from a List Using Sets


Theory:
In Python, a list is a collection that allows duplicate elements. A set, on the other hand, is an
unordered collection that automatically removes duplicates because it only allows unique
elements. Using a set to remove duplicates from a list is efficient because the conversion
process inherently filters out duplicate elements.
Steps to Remove Duplicates:
1. Convert the list into a set, which will eliminate duplicates.
2. If necessary, convert the set back into a list to maintain the list structure.
3. This approach maintains time complexity close to O(n)O(n) for the conversion.
Advantages:
 Simple and efficient.
 Eliminates all duplicates in one step.
 Set operations are faster compared to list operations for removing duplicates.

Python Program:
# Function to remove duplicates from a list using set
def remove_duplicates(input_list):
print(f"Original list: {input_list}")
unique_set = set(input_list) # Convert list to set
unique_list = list(unique_set) # Convert set back to list
print(f"List after removing duplicates: {unique_list}")
return unique_list

# Test case
input_list = [1, 2, 3, 2, 4, 1, 5, 4, 6]
remove_duplicates(input_list)
Example Execution:
Input:
input_list = [1, 2, 3, 2, 4, 1, 5, 4, 6]
Output:
Original list: [1, 2, 3, 2, 4, 1, 5, 4, 6]
List after removing duplicates: [1, 2, 3, 4, 5, 6]

Considerations:
 If maintaining the original order is important, sets won't work because they are
unordered.
 To preserve order, a different approach involving a loop and a temporary set to track
seen elements may be required.

You might also like