0

Let's say I have the following list:

my_list = ['aaa', 'bb_bb', 'cc', 2]

I would like to remove underscore _ from a list element and would like to get

my_list = ['aaa', 'bbbb', 'cc', 2]

I tried this

my_list= [re.sub('_', '', _) for _ in my_list ]

For some reason I am getting an error TypeError: expected string or bytes-like object.

Can anyone help with this?

2
  • 1
    You have an integer in the list. Commented Aug 12, 2022 at 10:39
  • 1
    The last element of the list is an int and re.sub() knows nothing about it. Commented Aug 12, 2022 at 10:40

2 Answers 2

7

It's because one of the elements is neither string nor bytes.

Also, you do not need re.sub to replace all instances of a character in a string, use str.replace instead, plus using _ as a variable name usually means it is ignored.

Try this instead:

[v.replace('_', '') if isinstance(v, str) else v for v in my_list]
Sign up to request clarification or add additional context in comments.

Comments

1

Or if you absolutely must/want to stick to regex:

import re
my_list = ['aaa', 'bb_bb', 'cc', 2]
my_list= [re.sub('_', '', value) if isinstance(value, str) else value for value in my_list]
print(my_list)

Result:

['aaa', 'bbbb', 'cc', 2]

2 Comments

Yes, but it's much slower (and more computationally expensive), timing the str.replace method gives 1.42 µs ± 132 ns while the re.sub method gives 5.64 µs ± 113 ns.
@ljmc Yes, I know. Maybe some day it's not underscore anymore, but something more complicated, then regex could be something to consider. I'm just leaving this answer as someone else might need it in the future. I upvoted your answer too as I think it's better for this. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.