1

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?

2 Answers 2

4

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
  • @caiovillaca - Most welcome! Glad it helped :) Commented Jun 11, 2019 at 10:36
2

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 ?

1
  • yep, I need it to read the values from the list... Commented Jun 10, 2019 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.