Filtering Duplicate Lists
Advanced Techniques for Removing Duplicates
Preserving Original Order
When you need to maintain the original sequence while removing duplicates, traditional set conversion won't work:
def remove_duplicates(input_list):
seen = set()
result = []
for item in input_list:
if item not in seen:
seen.add(item)
result.append(item)
return result
## Example usage
original_list = [3, 1, 4, 1, 5, 9, 2, 6, 5]
unique_ordered = remove_duplicates(original_list)
print(unique_ordered) ## Output: [3, 1, 4, 5, 9, 2, 6]
Filtering Complex Data Structures
Unique Elements in List of Dictionaries
def unique_dicts_by_key(input_list, key):
seen = set()
unique_list = []
for item in input_list:
if item[key] not in seen:
seen.add(item[key])
unique_list.append(item)
return unique_list
## Example with complex data
employees = [
{'id': 1, 'name': 'Alice'},
{'id': 2, 'name': 'Bob'},
{'id': 1, 'name': 'Charlie'},
{'id': 3, 'name': 'David'}
]
unique_employees = unique_dicts_by_key(employees, 'id')
print(unique_employees)
Filtering Strategies Comparison
Method |
Complexity |
Order Preservation |
Memory Efficiency |
set() |
O(n) |
No |
High |
List Comprehension |
O(nยฒ) |
Yes |
Moderate |
Dictionary Method |
O(n) |
Yes |
High |
flowchart TD
A[Input List] --> B{Filtering Method}
B --> |Simple set()| C[Fastest Conversion]
B --> |Custom Function| D[Flexible Filtering]
B --> |Comprehension| E[Ordered Result]
Handling Nested Structures
def unique_nested_list(nested_list):
return list(map(list, set(map(tuple, nested_list))))
## Example of unique nested lists
complex_list = [[1, 2], [3, 4], [1, 2], [5, 6]]
unique_nested = unique_nested_list(complex_list)
print(unique_nested) ## Output: [[1, 2], [3, 4], [5, 6]]
Best Practices for LabEx Learners
- Choose the right method based on your specific use case
- Consider performance implications
- Understand the trade-offs between different filtering techniques
By mastering these techniques, LabEx students can efficiently handle duplicate filtering in various Python scenarios.