I have been using this script to select a feature using Python:

layer = iface.activeLayer()
layer.selectByExpression('\"Declividad\"= value', QgsVectorLayer.SetSelection)
selection = layer.selectedFeatures()

But now, I need to select the values for Declividad from a list.

For exemple: list = [10,11,12], so I want to select the values 10,11 and 12 for Declividad.

How could I do that?


You could juse use an expression like:

layer = iface.activeLayer()
my_list = [10, 11, 12]
values = ','.join(str(x) for x in my_list)
layer.selectByExpression('\"Declividad\" IN (' + values + ')', QgsVectorLayer.SetSelection)
  • 1
    Oh! that did the trick :) thanks! – caio villaca Jun 10 at 15:02
  • @caiovillaca - Most welcome! Glad it helped :) – Joseph Jun 11 at 10:36

You can try the IN operator :

layer.selectByExpression('\"Declividad\" IN (\'10\',\'11\','\12')', QgsVectorLayer.SetSelection)

Or do u need to dynamically reference the list name ?

  • yep, I need it to read the values from the list... – caio villaca Jun 10 at 14:51

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.