I'm am new to using PyQGIS and this question has been answered in various forms already, however not specifically the way I need it; I have a big QGIS project with several layers with similar, but not the same names (e.g. names containing certain keywords like "nationalpark", "flooding zone" etc.), and I want to apply Styles to all layers with names containing certain keywords, e.g. "nationalpark". I found out that I can select layers using e.g.

layers = QgsProject.instance().mapLayersByName('my layer name')

but how can I select layers by non-specific names, containing certain keywords?

1 Answer 1


You can use Python's in operator to retrieve the layers in your project whose names contain a keyword/words.


key_word = "flooding zone"

layers = [lyr for lyr in QgsProject.instance().mapLayers().values() if key_word in lyr.name()]


for lyr in layers:
    # Do something with layer...

The mapLayers() method of QgsProject returns a dictionary, so we use Python's values() method to access the QgsMapLayer objects.


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.