python - Trying to learn basic for loop and/or if statement -


i have started learn python , wonder wrong loop and/or if statement.

i trying loop through list of words , if find word "right" want replace word "left".

def left_join(phrases):                 """                      join strings , replace "right" "left"             """                     mlist = list(phrases)       word in mlist:          if word == "right":              word = "left"      output = ','.join(mlist)     return output      phrases = ("left", "right", "left", "stop") left_join(phrases)   

i supposed

"left,left,left,stop"

but get

"left,right,left,stop"

so "right" not replaced "left". why?

quoting official documentation on loop,

an iterator created result of expression_list. suite executed once each item provided iterator, in order of ascending indices.

so mlist not change, word changed here


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -