Efficient way in Python to add an element to a comma-separated string
        Posted  
        
            by ensnare
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by ensnare
        
        
        
        Published on 2010-05-20T04:34:35Z
        Indexed on 
            2010/05/20
            4:40 UTC
        
        
        Read the original article
        Hit count: 332
        
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'
addition = 'Cherries'
result = 'Apples, Bananas, Cherries, Grapes, Oranges'
Also, a way to do this but while maintaining IDs:
string = '1:Apples, 4:Bananas, 6:Grapes, 23:Oranges'
addition = '62:Cherries'
result = '1:Apples, 4:Bananas, 62:Cherries, 6:Grapes, 23:Oranges'
Sample code is greatly appreciated. Thank you so much.
© Stack Overflow or respective owner