I am trying to start with PyQGIS and make a simple script in the processing toolbox.

##theVector=vector theVec = processing.getObject(theVector) features = theVec.getFeatures()

now, if I'd like to iterate over every Object I'd write:

for feature in features:

but I just want to get the first object, and NOT interate over everyone.. I have tried:

f1 = features.get(1)

and get

'QgsFeatureIterator' object has no attribute 'get' See log for more details

How do I get just one feature?

  • Welcome to gis.se. Please edit your thread title to a meaningful question which provides enough details for future readers who might come here with a similar problem looking for solutions.
    – underdark
    Commented Jan 5, 2015 at 17:58
  • ad documentation: check gis.stackexchange.com/questions/3651/…
    – underdark
    Commented Jan 5, 2015 at 18:02
  • Thank you underdark, I will check that and keep your advise about the title. Commented Jan 8, 2015 at 2:46

2 Answers 2


It is a pure Python problem not limited to PyQGIS. I learn Python before using PyQGIS and the Help of PyQGIS.

In pure Python examine the attributes of feature with the dir() function:

['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'close', 'isClosed', 'next', 'nextFeature', 'rewind']

And there is no get here, hence the error: QgsFeatureIterator' object has no attribute 'get' See log for more details when you try f1 = features.get(1).

In contrats, features has the __iter__ method which means that you can use a for loop, look at Understanding Python Iterables and Iterators

When Python executes the for loop, it first invokes the iter() method of the container to get the iterator of the container. It then repeatedly calls the next() method (next() method in Python 3.x) of the iterator until the iterator raises a StopIteration exception. Once the exception is raised, the for loop ends. (when Python executes the for loop, it first invokes the iter() method of the container to get the iterator of the container)

So for feature in features is the same as features.next(), features.next(),...

Therefore the answer of Underdark features.next() is a pure Python problem, not limited to the Help of PyQGIS (The same kind of problem existed with Avenue, even with the Help)


The simplest approach to get the first feature would be:

>>> f1 = features.next()

When you type help (features) you'll get the help:

Help on QgsFeatureIterator in module qgis._core object: class QgsFeatureIterator(sip.wrapper) | QgsFeatureIterator() | QgsFeatureIterator(QgsFeatureIterator) | | Method resolution order: | QgsFeatureIterator | sip.wrapper | sip.simplewrapper | __builtin__.object | | Methods defined here: | | __iter__(...) | x.__iter__() <==> iter(x) | | close(...) | QgsFeatureIterator.close() -> bool | | isClosed(...) | QgsFeatureIterator.isClosed() -> bool | | next(...) | x.next() -> the next value, or raise StopIteration | | nextFeature(...) | QgsFeatureIterator.nextFeature(QgsFeature) -> bool | | rewind(...) | QgsFeatureIterator.rewind() -> bool

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.