Strikethrough applied to original question before edits, left for posterity:
You're not using a dict
at all, just a list
of two-tuple
s, where the second element in each tuple
is itself a list
. If you actually want a dict
,
dict([('769817', [6]), ('769819', [4, 10]), ('769819', [4, 10])])
will convert it, and uniquify by key (so you'd end up with {'769817': [6], '769819': [4, 10]}
, though it loses order, and doesn't pay attention to whether the values (the sub-list
s) are unique or not (it just keeps the last pairing for a given key).
If you need to uniquify adjacent duplicates (where the values are important to uniqueness) while preserving order, and don't want/need a real dict
, use itertools.groupby
:
import itertools
nonuniq = [('769817', [6]), ('769819', [4, 10]), ('769819', [4, 10])]
uniq = [k for k, g in itertools.groupby(nonuniq)]
# uniq is [('769817', [6]), ('769819', [4, 10])]
# but it wouldn't work if the input was
# [('769819', [4, 10]), ('769817', [6]), ('769819', [4, 10])]
# because the duplicates aren't adjacent
If you need to collapse non-adjacent duplicates, and don't need to preserve order (or sorted order is fine), you can use groupby
to get a O(n log n)
solution (as opposed to naive solutions that create a new list and avoid duplicates by checking for presence in the new list at O(n^2)
complexity, or set
based solutions that would be O(n)
but require you to convert sub-list
s in your data to tuple
s to make them hashable):
# Only difference is sorting nonuniq before grouping
uniq = [k for k, g in itertools.groupby(sorted(nonuniq))]
# uniq is [('769817', [6]), ('769819', [4, 10])]