Getting list of layer names using PyQGIS?

QgsMapLayerRegistry.instance().mapLayers() will give you all layers that are opened.

If you want the names then:

names = [layer.name() for layer in QgsMapLayerRegistry.instance().mapLayers().values()]

names will be a list of layer names

or using a normal function:

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    print layer.name()

Since version 3, QgsMapLayerRegistry funcionalities have been moved to QgsProject: https://qgis.org/api/api_break.html

Update for QGIS3.x:

from qgis.core import QgsProject
names = [layer.name() for layer in QgsProject.instance().mapLayers().values()]
print(names)

as per @Nathan W's answer, this produces a list of layers in the current project:

['GoogleSat', 'MyPointsLayer', 'Roads', 'House_numbers']

Tags:

Pyqgis