Skip to content Skip to sidebar Skip to footer

Getting Error While Using Itertools In Python

This is the continuation of the OP1 and OP2. Specifically, the objective is to remove duplicates if more than one dict has the same content for the key paper_title. However, the li

Solution 1:

Thanks to @Chris for pointing about the existence of str in test_list instead of dict ("all_result")

To address whereby sorted is raise an error that it cannot use f for str, the str need to be removed from the list.

As of OP, the str can be removed by

list(filter('all_result'.__ne__, test_list))

Note that, for this case, the str only have the value of 'all_result'.

The complete code then

defextract_secondary():

        test_list = [{"paper_title": 'This is duplicate', 'Paper_year': 2}, \
                     {"paper_title": 'This is duplicate', 'Paper_year': 3}, \
                     {"paper_title": 'Unique One', 'Paper_year': 3}, \
                     {"paper_title": 'Unique two', 'Paper_year': 3},'all_result','all_result']
        test_list=list(filter('all_result'.__ne__, test_list))
        f = lambda x: x["paper_title"]
        already_removed = [next(g) for k, g in groupby(sorted(test_list, key=f), key=f)]

extract_secondary()

Post a Comment for "Getting Error While Using Itertools In Python"