How to remove an element from a repeating protobuf field in python?

I have a protobuf message that contains a repeating field. I would like to remove one of the elements in the list, but I cannot find a good way to do this without copying all the elements from the repeating field to the list, clearing the repeating field and re-filling it.

In C ++, there is a RemoveLast() function, but it does not look like the python API ...

+6
source share
3 answers

As noted in the documentation , the object that wraps the repeating field in Protobuf behaves like a regular Python sequence. Therefore you should just do

 del foo.fields[index] 

For example, to remove the last item,

 del foo.fields[-1] 
+10
source

In Python, removing an item from a list can be done as follows:

 list.remove(item_to_be_removed) 

or

 del list[index] 
+1
source
 const google::protobuf::Descriptor *descriptor = m_pMessage->GetDescriptor(); const google::protobuf::Reflection *reflection = m_pMessage->GetReflection(); const google::protobuf::FieldDescriptor* field = descriptor->FindFieldByName("my_list_name"); if (i<list_size-1) { reflection->SwapElements(m_pMessage, field, i, list_size-1); } reflection->RemoveLast(m_pMessage, field); 
+1
source

All Articles