Sometimes, while working with String list, we can have a problem in which we need to remove the surrounding stray characters or noise from list of digits. This can be in form of Currency prefix, signs of numbers etc. Let’s discuss a way in which this task can be performed.
Method : Using list comprehension +
strip() + isdigit() + join()
The combination of above functions can be used to perform this task. In this, we strip the stray characters from numbers that are identified from the strings and return the result.
The original list : ['-4', 'Rs 25', '5 kg', '+15'] List after removing stray characters : ['-', '4', 'Rs', '25', '5', 'kg', '+', '15']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.