Skip to content Skip to sidebar Skip to footer

Avoid Inserting Duplicates Into Python List With Comprehension

I have a dictionary: XY_dict = {1: [(12, 55),(13, 55)], 2: [(14, 55),(15, 57)], 3: [(14, 55),(15, 58)], 4: [(14, 55),(16, 55)]} I want to find out which keys have values tuples of

Solution 1:

Others have already explained what's the problem with your list comprehension. Here's an alternative approach, using a Counter dictionary to count how often the different xy pairs occur and using that to filter the unique entries from the dictionary.

>>>from collections import Counter>>>c = Counter(xy for v in XY_dict.values() for xy in v)>>>{k: v for k, v in XY_dict.iteritems() ifall(c[xy] == 1for xy in v)}
{1: [(12, 55), (13, 55)]}

Or to get the keys with shared values:

>>>[k for k, v in XY_dict.iteritems() ifany(c[xy] > 1for xy in v)]
[2, 3, 4]

Note that this is also more efficient, as you compare each combination of two items from the dictionary, giving you quadratic complexity, while this approach has linear complexity.

Solution 2:

The problem is that keys_shared_values is empty until you complete the comprehension, so your k1 not in keys_shared_values will always return True. You cannot refer to the current comprehension. Your best bet is to convert to set as you already suggested.

You should change your code to a loop if you want that functionality:

keys_shared_values = []
fork, v in XY_dict.iteritems():
    fork1, v1 in XY_dict.iteritems():
        forXY_pairin v:
            if XY_pair in v1 and k != k1 and k1 not in keys_shared_values:
                keys_shared_values.append(k1)
print keys_shared_values

result:

[3, 4, 2]

Solution 3:

Your code cannot work because key_shared_values is not defined. If you clean up your environment you will see that if you try to run your example you will get a NameError: name 'key_shared_values' is not defined error.

This is because keys_shared_values is not really defined until the comprehension statement runs, you cannot really reference it within the comprehension because it doesn't already exist.

If you were to predefine it, for example as keys_shared_values = [] then this would still not work, because every time you would reference it in the comprehension it would reference the original empty list value. When the comprehension is executed it doesn't dynamically change the value of keys_shared_values, instead it creates the list in memory and then assigns it to keys_shared_values.

Post a Comment for "Avoid Inserting Duplicates Into Python List With Comprehension"