LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

Working on removing characters of the strings in a list but do not get the expected ouput. I am on the mission and step referred below

Screen Link:

https://app.dataquest.io/m/351/cleaning-and-preparing-data-in-python/7/parsing-numbers-from-complex-strings-part-one

My Code:


bad_chars = ["(",")","c","C",".","s","'", " "]
9
def strip_characters(test_data):
10
    for char in bad_chars:
11
        test_data = test_data.replace(char,"")
12
        return test_data
13
    
14
stripped_test_data = []
15
for d in test_data:
16
    date = strip_characters(d)
17
    stripped_test_data.append(date)

What I expected to happen:
I expected all the “bad” characters in the strings of a list to be removed

What actually happened:
is that only first bad character, “(“, in the first string of the list was removed while the rest of the “bad” characters remain in the strings of the list


stripped_test_datalist (<class 'list'>)
- actual + expected

  ['1912',
   '1929',
   '1913-1923',
-  '1951)',
+  '1951',
   '1994',
   '1934',
-  'c. 1915',
+  '1915',
   '1995',
-  'c. 1912',
-  '1988)',
+  '1912',
+  '1988',
   '2002',
   '1957-1959',
-  'c. 1955.',
-  "c. 1970's",
-  'C. 1990-1999']
+  '1955',
+  '1970',
+  '1990-1999']

Hey, Eduardo.

It’s hard to understand your code, can you please format it properly?

In any case, I’ll take a stab at answering the question by guessing your intended code. It seems like you’re returning the result too soon, if you return inside the loop, the function will exit right at the first iteration.

I hope this helps.

Thanks, Bruno. I cleared the mission. Sorry for the “not formatting”. I Just copy-pasted from the coding screen. This is too new for me right now. I will try to improve my language/messages as I progress.
Regards