Delete items from the list based on an item field

advertisements

I have a list of tuples where each tuple has two items; first item is a dictionary and second one is a string.

all_values = [
              ({'x1': 1, 'y1': 2}, 'str1'),
              ({'x2': 1, 'y2': 2}, 'str2'),
              ({'x3': 1, 'y3': 2}, 'str3'),
              ({'x4': 1, 'y4': 2}, 'str1'),
             ]

I want to remove duplicate data from list based on the second item of the tuple. I wrote this code, but I want to improve it:

flag = False
items = []
for index, item in enumerate(all_values):
    for j in range(0, index):
        if all_values[j][1] == all_values[index][1]:
            flag = True
    if not flag:
        items.append(item)
    flag = False

And get this:

items = [
         ({'x1': 1, 'y1': 2}, 'str1'),
         ({'x2': 1, 'y2': 2}, 'str2'),
         ({'x3': 1, 'y3': 2}, 'str3')
        ]

Any help?

BTW I tried to remove duplicate data using list(set(all_values)) but I got error unhashable type: dict.


Use another list ('strings') to collect the second string items of the tuples. Thus, you will have a clear way to check if a current list item is a duplicate.

In the code below I added one duplicate list item (with 'str2' value) for demonstration purpose.

all_values = [
              ({'x1': 1, 'y1': 2}, 'str1'),
              ({'x2': 1, 'y2': 2}, 'str2'),
              ({'x5': 8, 'ab': 7}, 'str2'),
              ({'x3': 1, 'y3': 2}, 'str3')
             ]

strings = []
result = []
for value in all_values:
    if not value[1] in strings:
        strings.append(value[1])
        result.append(value)

The new non-duplicated list will be in 'result'.