You can simply do:
features = l.getFeatures('"Counties" = \'Norwich\'')
The QGIS expression engine is also able to do this using the QgsFeatureRequest.setFilterExpression( unicode str)
method (Since QGIS 2.2)
request = QgsFeatureRequest().setFilterExpression( u'"Counties"'"Counties" = \'Norwich\'' )
it = l.getFeatures( request )
Starting with QGIS 2.10 it is even possible that filtering in such a way will give you some extra performance over other types of filtering (like python implementations).
Basically this applies if the following three conditions are met:
You are using a layer with the postgis providerAt the moment (2.16) many more than just postgis provider implement this (spatialite, ogr, oracle...).- Your expression is not overly complicated (things like
>
,=
,IN
,NOT NULL
... are supported) - You have enabled this feature in Settings > Options > Data Sources > Data Source Handling > Execute expression on postgres server-side
- The performance benefit is optimal with appropriate indexes on the database tables
With QGIS 3.0 it is even possible to simply do
features = l.getFeatures('"Counties" = \'Norwicth\'')