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

Help On the basics. Why can't I use an alias here?

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

My Code:

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

bad_chars = ["(",")","c","C",".","s","'", " "]

def strip_characters(string):
    for char in bad_chars:
        cleaned_string = string.replace(char,'') 
    return cleaned_string


stripped_test_data =[]

for data in test_data:
    cleaned_data = strip_characters(data)
    stripped_test_data.append(cleaned_data)
    
print(stripped_test_data)   

What I expected to happen:

['1912', '1929', '1913-1923', '1951', '1994', '1934', '1915', '1995', '1912', '1988', '2002', '1957-1959', '1955', '1970', '1990-1999']

What actually happened:

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

Firstly, Sorry for all these stupid questions. But I really want to hone the basics and understand how does everything work. So I very much appreciate the quick responses.
Secondly, Coming to the problem , Why does the code below works and mine does not.

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

bad_chars = ["(",")","c","C",".","s","'", " "]

def strip_characters(string):
    for char in bad_chars:
        string = string.replace(char,'') 
    return string


stripped_test_data =[]

for data in test_data:
    cleaned_data = strip_characters(data)
    stripped_test_data.append(cleaned_data)
    
print(stripped_test_data)   

Hello @ShubhamLohia,

there is a minor difference in the code but it makes a large difference in the outcome.

def strip_characters(string):
    for char in bad_chars:
        cleaned_string = string.replace(char, '')   # Here's the culprit (cleaned_string)
return cleaned_string

With each new char from bad_chars checked, the cleaned_string is not updated but replaced with the initial value, e.g.

string = β€˜(1951)’

# Here are the first 3 loops of strip_characters:

index = 0
bad_chars = ["(",")",β€œc”,β€œC”,".",β€œs”,"’", " "]
cleaned_string = β€˜1951)’ (the left parenthesis is removed)

index = 1
bad_chars = ["(",")",β€œc”,β€œC”,".",β€œs”,"’", " "]
cleaned_string = β€˜(1951’ (The right parenthesis is removed)

index = 2
bad_chars = ["(",")","c",β€œC”,".",β€œs”,"’", " "]
cleaned_string = β€˜(1951)’ (No c in the string, so nothing is removed)

So in the end only the last character in bad_chars will be removed properly. To work properly, the new string must be updated, i.e.:

cleaned_string = cleaned_string.replace(char.'')

would work too, but you would need to assign the input string to cleaned_string, which would work but make the code slightly less efficient and longer.

If you have any questions, I’ll do my best to help. :slight_smile:

3 Likes

@ShubhamLohia First of all, never be sorry for desiring to learn. No question is stupid and always remember, we are all learning and need each other, I also had the same error. Now… onto your question. When you look at your output(stripped_test_data), it is the same as your input(test_data). This means that the function is not working as per the desired purpose.

  • Let’s look at your function:

      def strip_characters(string):
          for char in bad_chars:
              cleaned_string = string.replace(char,'') 
          return cleaned_string
    

In the function strip_characters, You need to make changes to the string parameter directly so that the output becomes the new string to which you have performed the operations. Use the below function instead.

def strip_characters(string):
    for char in bad_chars:
        string = string.replace(char,'') 
    return string

Summary: The operations are to be carried out directly to the input in order to affect the output. I hope that helps. Please feel free to reach out if you still need clarification.

1 Like

Thank You :heart:. I understand where I went wrong.

1 Like

Thank you :heart: for this wonderful explanation with a help of a example and step by step process. :+1:t2::ok_hand:t2:

2 Likes