Some thought about 10.censor


#1



So my code is as below, I am aware it's not the correct code. I am just curious when I use "for each" loop I can't actually modify the list? Because I am studying Java in my university now and it told me in java "for each" loop, the n is a changing variable, it doesn't really refer to the actual element in the list? Is that the same in python?


def censor(text, word):
    new_sentence = ""
    parts = text.split()
    for n in parts:
        if n == word:
            n = "*" * len(word)
    print parts
    
    new_sentence = " ".join(parts)
    return new_sentence
censor("hey hey hey", "hey")


#2

yes, it is the same in python. n gets assigned the value from the list, but i suspect it is a copy of the value which is stored in the list


#3

I am not sure it will be stored in the list as a copy because it's not very efficient if it does. I think it's more like the n exists only when the for each loop code is being excuted. Anyway, thx for helping me mate!