I would like to do a very simple task: (1) Add a field to a layer and (2) add some values to the field.

I have ADDED A FIELD TO A LAYER by the following code and created three new fields: Dev_site, Buffer and Unit.

enter image description here

Now I want to add values to the field using python. I searched online and found the field calculator tool:

processing.runalg('qgis:fieldcalculator', input_layer, field_name, field_type, field_length, field_precision, new_field, formula, output_layer).

However, in my case, the field calculator is a little bit advanced. I just want to add some very simple string and number to the field which doesn't need any formula.

Is there an alternative (simpler) way to add values to the field using PyQGIS?

1 Answer 1


You can try this approach:

The basic workflow is:

-Lookup index values of fields in your attribute table.

-Create a dictionary containing the field indexes as the keys and the attribute information as the values.

-Call changeAttributeValues() on the layer data provider, passing another dictionary containing a feature id as the key and the attribute map dictionary as the value.

layer = QgsProject().instance().mapLayersByName('Campus_5m')[0]
prov = layer.dataProvider()
# lookup index of fields using their names
dev_site_idx = layer.fields().lookupField('Dev_Site')
buff_idx = layer.fields().lookupField('Buffer')
unit_idx = layer.fields().lookupField('Unit')
# create a dictionary with field index as key and the attribute you want as value
atts = {dev_site_idx: 'Residential', buff_idx: 1000, unit_idx: 'Meters'}
# store reference to feature you want to update
feat = layer.getFeature(1)
# call changeAttributeValues(), pass feature id and attribute dictionary
prov.changeAttributeValues({feat.id(): atts})

You should see the following result:

enter image description here

  • That is what I exactly need! Thanks a lot. Also, may I ask how should we know which function to call (e.g., lookupField, changeAttributeValues). Did you learn them from Developer Cookbook?
    – rui jiang
    Commented Jul 28, 2019 at 9:48
  • No problem @rui jiang, I'm glad it helped! I just revised my answer slightly as I realised I could simplify the code a bit- create a single dictionary of field indexes and attributes instead of a list of dictionaries. Much simpler! As for reference material- yes, the Pyqgis Developer Cookbook is good. See the section on Modifying Vector Layers: docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/…. I can also recommend The PyQGIS Programmer's Guide for QGIS 3 by project founder Gary Sherman.
    – Ben W
    Commented Jul 29, 2019 at 6:14
  • Also, don't underestimate the value of consulting the API documentation. In this case the QgsVectorLayer, QgsFields and QgsVectorDataProvider classes: qgis.org/api/classQgsVectorLayer.html | qgis.org/api/classQgsFields.html | qgis.org/api/classQgsVectorDataProvider.html
    – Ben W
    Commented Jul 29, 2019 at 6:17

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.