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?

Snaileater
5,8031 gold badge18 silver badges27 bronze badges
asked Jun 10, 2019 at 14:25

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)
answered Jun 10, 2019 at 14:43
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 ?

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.