import re
import numpy as np
numlist=["$10000","$20,000","30,000",40000,"50000 "]
help(re.sub)
Help on function sub in module re: sub(pattern, repl, string, count=0, flags=0) Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used.
for i,value in enumerate(numlist):
numlist[i]=re.sub(r"([$,])","",str(value))
numlist[i]=int(numlist[i])
Here re.sub replaces the $ and , patterns with nothing ("") from each value of the list just like gsub does in R. In python str converts an object to string just like paste does in R. In the next step, int converts the object to numeric values (integer) just as as.numeric does in R
print(numlist)
[10000, 20000, 30000, 40000, 50000]
np.mean(numlist)
30000.0
help(enumerate)
Help on class enumerate in module builtins: class enumerate(object) | enumerate(iterable[, start]) -> iterator for index, value of iterable | | Return an enumerate object. iterable must be another object that supports | iteration. The enumerate object yields pairs containing a count (from | start, which defaults to zero) and a value yielded by the iterable argument. | enumerate is useful for obtaining an indexed list: | (0, seq[0]), (1, seq[1]), (2, seq[2]), ... | | Methods defined here: | | __getattribute__(self, name, /) | Return getattr(self, name). | | __iter__(self, /) | Implement iter(self). | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | __next__(self, /) | Implement next(self). | | __reduce__(...) | Return state information for pickling.
Here we used enumerate to replace and convert each value of the list one by one. This is a powerful method unique to Python as for loops are not computationally efficient in R.