Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove an item from a repeated protobuf field in python?

I have a protobuf message that contains a repeated field. I would like to remove one of the items in the list but I can't seem to find a good way to do so without copying all of the items out of the repeated field into a list, clearing the repeated field, and repopulating it.

In C++ there is a RemoveLast() function, but this doesn't seem to appear in the python API...

like image 831
Dereck Wonnacott Avatar asked Mar 09 '13 04:03

Dereck Wonnacott


1 Answers

As noted in the documentation, the object wrapping a repeated field in Protobuf behaves like a regular Python sequence. Therefore, you should be able to simply do

del foo.fields[index]

For example, to remove the last element,

del foo.fields[-1]
like image 167
nneonneo Avatar answered Oct 05 '22 05:10

nneonneo