My layer has an attribute 'name' which is unique for the layer and enforced by the database. I have been looking for an API call that will return a list of features that match the value of the name attribute but can't find anything.

Is there a more efficient way of finding the feature by attribute value than simply iterating over all the features in the layer?

I am guessing not since this implies QGIS maintains some form of index which I doubt!

closed as off-topic by PolyGeo Nov 15 '18 at 5:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.


underdark's suggestion works well and would be great for large sets of data but was only a marginaly faster for my cases where I had less than 1000 points in the layer. What I ended up doing was reading the whole layer and building a dictionary indexed by name. Once done it was very fast to find individual entries.

It worked because I was repeatedly looking for exact matches, if you want to do searches that return multiple possibilities then the filter option is better.

As usual with things like this there are tradeoffs depending on exactly what you need to do

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