Efficient way in Python to remove an element from a comma-separated string

Posted by ensnare on Stack Overflow See other posts from Stack Overflow or by ensnare
Published on 2010-05-20T07:57:24Z Indexed on 2010/05/20 8:00 UTC
Read the original article Hit count: 213

I'm looking for the most efficient way to add an element to a comma-separated string while maintaining alphabetical order for the words:

For example:

string = 'Apples, Bananas, Grapes, Oranges'
subtraction = 'Bananas'
result = 'Apples, Grapes, Oranges'

Also, a way to do this but while maintaining IDs:

string = '1:Apples, 4:Bananas, 6:Grapes, 23:Oranges'
subtraction = '4:Bananas'
result = '1:Apples, 6:Grapes, 23:Oranges'

Sample code is greatly appreciated. Thank you so much.

© Stack Overflow or respective owner

Related posts about python

Related posts about string-manipulation