How do I either avoid adding duplicate entries into a generator or remove them once there are already there?
If I should be using something else, please advice.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the values are hashable, the simplest, dumbest way to remove duplicates is to use a
set:But watch out: sets don’t remember what order the values were originally in. So this scrambles the sequence.
The function below might be better than
setfor your purpose. It filters out duplicates without getting any of the other values out of order:Call
nubwith one argument, any iterable of hashable values. It returns an iterator that produces all the same items, but with the duplicates removed.