I have a layer and I would like to access the number of rows the attribute table has from my code. Is there a way to do that without doing it like this:

Index = 0

for Feat in Layer.getFeatures():
    Index += 1

print Index

Yes, there is a direct way to access the number of rows of your attribute table. Just call featureCount() from your layer object:

| improve this answer | |

Layer.getFeatures() is a Python Iterator (type 'QgsFeatureIterator') so

index = len(list(layer.getFeatures()))

or you can use List comprenhesion

index = len([feature for feature in layer.getFeatures()])
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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