Remove Duplicates
In [1]:
# Removing Duplicates from a List of Integers
int_list = [1, 2, 2, 3, 4, 4, 5]
unique_int_list = list(set(int_list))
print(unique_int_list)
[1, 2, 3, 4, 5]
In [2]:
# Removing Duplicates from a List of Strings, preserving order
str_list = ["apple", "banana", "apple", "cherry", "banana"]
unique_str_list = list(dict.fromkeys(str_list))
print(unique_str_list)
['apple', 'banana', 'cherry']
In [3]:
# Removing Duplicates from a List of Dictionaries
dict_list = [
{"name": "John", "age": 30},
{"name": "Jane", "age": 25},
{"name": "John", "age": 30}
]
unique_dict_list = [dict(t) for t in {frozenset(d.items()) for d in dict_list}]
print(unique_dict_list)
[{'age': 25, 'name': 'Jane'}, {'age': 30, 'name': 'John'}]
In [4]:
import pprint
# Removing Duplicates from Healthcare Data
healthcare_data = [
{"patient_id": 1, "name": "Alice", "condition": "Diabetes"},
{"patient_id": 2, "name": "Bob", "condition": "Hypertension"},
{"patient_id": 1, "name": "Alice", "condition": "Diabetes"}
]
unique_healthcare_data = {d['patient_id']: d for d in healthcare_data}.values()
pprint.pp(list(unique_healthcare_data))
[{'patient_id': 1, 'name': 'Alice', 'condition': 'Diabetes'},
{'patient_id': 2, 'name': 'Bob', 'condition': 'Hypertension'}]
