['0-0-0', '1-10-20', '3-10-15', '2-30-20', '1-0-5', '1-10-6', '3-10-30', '3-10-4']
How can I remove all the hyphens between the numbers?
Use the String. replace() method to remove all hyphens from a string, e.g. const hyphensRemoved = str. replace(/-/g, ''); . The replace() method will remove all hyphens from the string by replacing them with empty strings.
You can just iterate through with a for loop and replace each instance of a hyphen with a blank. This code should give you a newlist without the hyphens.
We can use the replace() function to remove apostrophes from string variables. To remove an apostrophe from a string, you can use replace() and pass an empty string as the replacement value as shown below. string_with_apostrophe = "I'm looking for the dog's collar." string_without_apostrophe = string_with_apostrophe.
You can just iterate through with a for loop and replace each instance of a hyphen with a blank.
hyphenlist = ['0-0-0', '1-10-20', '3-10-15', '2-30-20', '1-0-5', '1-10-6', '3-10-30', '3-10-4']
newlist = []
for x in hyphenlist:
newlist.append(x.replace('-', ''))
This code should give you a newlist without the hyphens.
Or as a list comprehension:
>>>l=['0-0-0', '1-10-20', '3-10-15', '2-30-20', '1-0-5', '1-10-6', '3-10-30', '3-10-4']
>>>[i.replace('-','') for i in l]
['000', '11020', '31015', '23020', '105', '1106', '31030', '3104']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With