To get field names with fields()
method or other field properties (length
, type
, comment
, ...) you can use:
field_names = [field.name() for field in layer.fields()]
# ['id', 'attr1', 'attr2', 'attr3']
If you just need names, it's sufficient to use:
field_names = layer.fields().names()
# ['id', 'attr1', 'attr2', 'attr3']