Search code examples
pythonliststrip

In Python, how can I remove characters around a specific character?


I have a list that looks like the following. Within each item of the list, separate items are delimited by a semicolon, but there the amount of space surrounding each semicolon seems to be random:

['New Jersey  ;   46.3% ;  Republican ;         03/10/2015', 'Pennsylvania ; 
      39.0%; Democrat    ;04/30/2012', 'Virginia .   ;54.7% ;Independent 
   ;10/25/10',       'Maryland;44.8%   ;        Democrat; 01/15/16', 'New York;  R50.9%; Republican ;                  09/22/15'] 

I would like the final output to be a list that looks like the following:

['New Jersey;46.3%;Republican;03/10/2015', 'Pennsylvania;39.0%;Democrat;04/30/2012', 'Virginia;54.7%;Independent;10/25/10' ... ]

I've been trying .split(), but that's not dropping the characters in the middle. Is doing a .replace() on each possible combination of spaces and semicolons my only hope?


Solution

  • Here's a short way of doing it. This one line should be enough.

    s = ['New Jersey  ;   46.3% ;  Republican ;         03/10/2015', 'Pennsylvania ; 39.0%; Democrat    ;04/30/2012', 'Virginia .   ;54.7% ;Independent ;10/25/10',       'Maryland;44.8%   ;        Democrat; 01/15/16', 'New York;  R50.9%; Republican ;                  09/22/15']
    
    new_list = [';'.join([word.strip() for word in item.split(';')]) for item in s]
    

    And here's the Expanded Form.

    new_list = []
    
    for item in s:
        sub_list = [word.strip() for word in item.split(';')]
        new_list.append(';'.join(sub_list))
    
    print(new_list)
    

    Outputs:

    ['New Jersey;46.3%;Republican;03/10/2015', 'Pennsylvania;39.0%;Democrat;04/30/2012', 'Virginia .;54.7%;Independent;10/25/10', 'Maryland;44.8%;Democrat;01/15/16', 'New York;R50.9%;Republican;09/22/15']