Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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
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 is what i was looking for. Thank you for the hint! – gallay Oct 8 '13 at 20:04

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():
share|improve this answer

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.fieldNameIndex('fieldName')
values = vectorLayer.uniqueValues( idx )
share|improve this answer

Late but maybe somebody can use it.

It is also posible to use the native QGIS 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
share|improve this answer
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
On the Python console? Where else??? – Jochen Schwarze 2 days ago

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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