For loop and append


I tried experimenting with this code:

begin_list =[“omega”, “beta”, “omega”]
end_list = []

for word in begin_list:
end_list.append(word + “help”)

print end_list

and it came out like this:

[‘omegahelp’, ‘betahelp’]
[‘omegahelp’, ‘betahelp’, ‘omegahelp’]

I cannot understand why it came out 3 times like this, can anyone explain?


you nested the print statement inside the loop, so each iteration of the loop it prints the list. Given you are appending each iteration of the loop, the list print each time has more elements


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.