[GIS] Inverting selection in PyQGIS

pyqgisselect-by-location

I want to select the features that are not equal. So, I made a selection by location where the geometric predicate is equal and then I thought to invert the selection. Is there any way to invert the selection using PyQGIS?

My code:

import processing
layer1 = processing.getObject('polygon1')
layer2 = processing.getObject('polygon2')

processing.runalg('qgis:selectbylocation', layer2, layer1, u'equals', 0.005,0)

#INVERT SELECTION

Best Answer

Assuming layer2 is the layer holding selection, just use:

layer2.invertSelection()
Related Question