#filtering multiple lists in 1 go using filter()
22 messages · Page 1 of 1 (latest)
i would just do something like this, but idk how to remove duplicates : ```py
posts = [{...}, {...}, {...}]
filtered_posts = []
filtered_posts.extend(list(filter(lambda x: x['test'] == True, posts)))
filtered_posts.extend(list(filter(lambda x: x['test1'] == True, posts)))
filtered_posts.extend(list(filter(lambda x: x['test2'] == True, posts)))```
this gives me an error : ```py
posts = res['data']['children'][1: 24]
filtered_posts = []
filtered_posts.extend(list(filter(check_nsfw, posts)))
filtered_posts.extend(list(filter(check_videos, posts)))
filtered_posts.extend(list(filter(check_gallerys, posts)))
filtered = list(set(filtered_posts))
``` TypeError: unhashable type: 'dict' i really dont wanto have to iterate through everything, is there anything else i can do?
i fixed that but it still dosent work, my filteres are not working properly for some reason
Why not use a for loop?
for tests in ["test1", "test2"]:
filtered_posts.extend(list(filter(lambda x: x[tests] == True, posts)))
I think a for loop would be cleaner way to do this, and that way you can check the duplicates, other way is to use a set.
what is tests? is it the function to filter with or what
because my functions are not as simple as what i have sent so i cannot do what you sent
It's the varible i used for the for loop?
for tests in [your tests]
i know
but either way i still have an issue removing the duplicates
since the list with duplicates is a list of dicts
so i cant use list(set([{...}, {...}]))
Because iirc you can't have two same keys in a dict
no
the list after all filtering should contain duplicates of some dicts
i need to remove those
Can you give an example on what you are trying to do
filter a list 3 times, using 3 diff functions