Getting unique values from multiple fields as matched using PyQGIS
You can use the class Counter
from the collections module.
from collections import Counter
layer = iface.activeLayer()
c = Counter([(feat['Code'],feat['Color']) for feat in layer.getFeatures()])
print(c) # Counter({('A', 'Red'): 3, ('B', 'Blue'): 2, ('C', 'Green'): 1})
combination = [comb for comb in c]
print(combination) # [('A', 'Red'), ('B', 'Blue'), ('C', 'Green')]
You can use set()
which is a Python built-in function.
layer = iface.activeLayer()
pairs = list(set([(f["Code"], f["Color"]) for f in layer.getFeatures()]))
print(pairs) # [('A', 'Red'), ('C', 'Green'), ('B', 'Blue')]
A set
is an unordered data structure, so it does not preserve the insertion order. You will probably get an unordered but a matched list (('A', 'Red') ('C', 'Green'), ..
).