Find Non-Duplicate Elements from List in Python
In Python, you can achive find non-duplicate elements from List as following:
1. For Integer List
nums = [1, 4, 8, 1, 11, 5, 4]
non_duplicate_list = [num for num in nums if nums.count(num) == 1]
print(non_duplicate_list)
This code will produce the output:
[8, 11, 5]
2. For String List
cities = ["Bengaluru", "Varanasi", "New Delhi", "Bengaluru"]
non_duplicate_list = [city for city in cities if cities.count(city) == 1]
print(non_duplicate_list)
This code will produce the output:
['Varanasi', 'New Delhi']