Take the 2-minute tour ×  Shouldn't the list comprehension
restrict the variable scope.  user = <user1> project.users =
[<user1>, <user2>, <user3>, <user4>] project_usernames  =
[user.username for user in project.users]  I am generating the
list project_usernames using list comprehension on
project.users. But it is modifying the user to which was earlier
.  I am using above flow in one of my project but because of
this bug it was not working.  later when I changed the variable
"user" in list comprehension, it worked properly.  entity within
<> refers to  I know that the interpreter works line by line,
but shouldn't the scope of variable used in list comprehension
die once the iteration is over?.  share|improve this question  1
Answer 1  up vote 1 down vote accepted  This is a Python 2.x
'feature', where the variable you use inside of the list
comprehension (in your case, user) becomes part of the
surrounding scope (in Python 3, it is treated like a generator -
see here for the breakdown from Guido himself). A