i look for solution to get the unique values of an attribute of a vector layer. what is the best way in python qgis to get the unique values of one distinct attribute? I work with qgis 1.8.0.

  • In QGIS without python you could just query the FieldCalculator with all unique values of a field (there is a button on the right side). A pythonic solution would also be possible. What do you need this value for? Please elaborate a bit – Curlew Oct 8 '13 at 17:42
  • i have polygons described by an attribute "group". Each group contains 1 to about 10 polygons. I want to process the polygons belonging to a certain group in for-loop, i.e. for each iteration of the for-loop i select the polygons with setSubsetString to process this group, e.g. to write them to a shape file. However i do not know the group befor reading the vector layer. – gallay Oct 8 '13 at 18:30
  • 2
    There is a split tool which will create separate shapefiles depending on an attribute if that's all you need. Otherwise, that plugin would also be the place to look for sample code. – underdark Oct 8 '13 at 18:58
  • From the plugin you were mentioned the function split in splitterdialog.py is what i was looking for. Thank you for the hint! – gallay Oct 8 '13 at 20:04

QGIS provides a simple API to get this information. You need to get the index of the field in question and then query the layer for unique values.

idx = vectorLayer.fields().indexOf('fieldName')
values = vectorLayer.uniqueValues(idx)
  • is there any way available to count total distinct values? – Muhammad Faizan Khan Sep 29 '16 at 12:53
  • Do you also know the QGIS3 solution? The QGIS3 'QgsVectorLayer' object has no attribute 'fieldNameIndex'. – joosthoek May 27 '19 at 13:45
  • Sure, updated... – Matthias Kuhn May 27 '19 at 13:49

Python's sets are handy for finding unique values. For example:

>>> set(range(4) + range(5) + range(6))
set([0, 1, 2, 3, 4, 5])

Try something like this (pyqgis 1.8):

values = set()
for feature in layer:
    index = layer.fieldNameIndex(ATTRNAME)

Pyqgis 2.0:

values = set()
for feature in layer.getFeatures():

Late but maybe somebody can use it.

It is also posible to use the native python function. The uniqueValues function of the child for vector layers of the class QgsDataProvider.

See the Qgis API: QgsDataProvider and QgsVectorDataProvider

    uniquevalues = [];
    uniqueprovider = layer.dataProvider()
    fields = uniqueprovider.fields()
    id = fields.indexFromName('nameOfField')
    uniquevalues = uniqueprovider.uniqueValues( id )
    for uv in uniquevalues:
       print uv

i do that from qgis phyton console.

  • 1
    Answer has been flagged as low quality. Could use a little more context and explanation - where would you enter this command, what and where does it output (a table?), etc. – Chris W Jul 17 '15 at 23:38
  • 1
    On the Python console? Where else??? – Jochen Schwarze Jul 25 '16 at 10:53
  • on python console. – saheka Sep 14 '16 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.