The following is of course working, selects these features of my_layer that have attribute 1 for my_field2 and appends the corresponding attributes of my_field1 in my_list:

my_layer = iface.activeLayer()
indexmf = my_layer.fieldNameIndex("my_field1")
my_list = []

it = my_layer.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"my_field2" = 1') )
my_layer.setSelectedFeatures( [ f.id() for f in it ] )
selection = my_layer.selectedFeatures()
for feature in selection:
    sfeat = feature.attributes()[indexmf]

#and of course remove selection

Then, I would like to create a function to do the above:

[edited.. it is working]

def my_function(layer, list, fieldname, attribute, index):
    it = layer.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"%s" = {0}'.format(attribute) %fieldname ) ) 
    layer.setSelectedFeatures( [ f.id() for f in it ] )
    selection = layer.selectedFeatures()
    for feature in selection:
        sfeat = feature.attributes()[index]

But when I call it nothing happens:

my_function(my_layer, my_list, "my_field2", 1, indexmf)

Could you please help me understand what I am missing?

2 Answers 2


It looks like your function does not work since you put your attributes into the string in the filter expression, which is why Python does not allocate the value you assign to it.

Replace this line

it = layer.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"fieldname" = attribute' ) )


it = layer.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"%s" = '+str(attribute) % fieldname ) )

  • I get an error: TypeError: unsupported operand type(s) for %: 'int' and 'str' , but I then modified it a little bit more like this: it = layer.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"%s" = {0}'.format(attribute) %fieldname ) ) , and finally it is working... thank you for your help!!
    – Eleftheria
    Nov 13, 2015 at 9:22
  • I was assuming that you were passing a string to the filter. If you wish to directly pass an integer you can replace %s (string) with %d (numeric or decimal)
    – B.Langley
    Nov 13, 2015 at 9:25
  • Even like this (%d) I get the same error.. Also tried your first expression (%s) with another field that has string values and then I get this error: TypeError: not all arguments converted during string formatting
    – Eleftheria
    Nov 13, 2015 at 9:35
  • Indeed I got confused there: you need to cast your attribute to a string by using str(attribute). Otherwise Python will not be able to concatenate the string with the attribute. Another way to do it would be u'"%s" = %d'% (fieldname,attribute)
    – B.Langley
    Nov 13, 2015 at 9:41
  • there seems to be a syntax error.. SyntaxError: invalid syntax
    – Eleftheria
    Nov 13, 2015 at 9:51

The number of parameters are different in the function definition and the function call, so your filter expression will filter nothing, because it will substiture indexmf into attribute in your function and index will be undefined.

I suppose your code would be clearer if your function would return the result list. For example

def my_function(layer, fieldname, attribute, index): list = [] ... list.append(...) return list

and calling the function:

mylist += my_function(....)

  • yes you are right about the number of arguments, but this was only a mistake when posting my question.. I just edited it to correct it.. also I had tried the return list argument just did not post it.. but thank you anyway!!
    – Eleftheria
    Nov 13, 2015 at 9:01

Your Answer

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

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