Ask coding questions

← Back to all posts
For Loop - Unexpected result
projectuwb1 (1)

Hey repl community,
Trying to work out what is wrong with my code in python.
File name: listadv1.py
names = ['dom', 'meg', 'kim', 'pete', 'jas', 'lyn']
for name in names:
print (f"You are invited to dinner, {name.title()}.")
names.remove(f"{name}")

Expected output: Prints "You are invited to dinner" and each name in the list
Actual output: Prints "You are invited to dinner" and only dom, kim and jas as outputs.

Could anyone tell me why?
I assumed that for each time the for loop executes, the name variable will be assigned the first value from the list and then when the code returns to execute the second time the second value from the list and so on..