Conclusion: don't modify the size of a list while iterating on this list, use a copy if necessary.

yes using a copy solves the problem thank you but I have couple of questions about the second method.
If I understood correctly

s[:] = (x for x in s if x != 'r')

means reconstruct the list s from the components of the list s itself (interating through them ) but skip the element 'r' right?
Also why can't we use just s instead of s[:].

s=(x for x in s if x!= 'r')
>>> s
<generator object <genexpr> at 0xa80ff54>

yes using a copy solves the problem thank you but I have couple of questions about the second method.
If I understood correctly

s[:] = (x for x in s if x != 'r')

means reconstruct the list s from the components of the list s itself (interating through them ) but skip the element 'r' right?
Also why can't we use just s instead of s[:].

s=(x for x in s if x!= 'r')
>>> s
<generator object <genexpr> at 0xa80ff54>

If you want to use s, write

s = [x for x in s if x!= 'r']
# or
s = list(x for x in s if x!= 'r')

there is a small difference with s[:] because this code create a new list for the name s. With s[:] it only fills the same list object with new elements. For example try this

a = ['b','a','r','r','i','s','t','e','r']
b = a
c = a
b = [x for x in b if x != 'r']
print a
c[:] = [x for x in c if x != 'r']
print a
print id(a), id(b), id(c)

If you want to learn more about generator expressions, read about generators and the iterator protocol in the python documentation, but you can postpone this a little if you are new to python.

s = [x for x in s if x!= 'r']
# or
s = list(x for x in s if x!= 'r')

there is a small difference with s[:] because this code create a new list for the name s. With s[:] it only fills the same list object with new elements. For example try this

a = ['b','a','r','r','i','s','t','e','r']
b = a
c = a
b = [x for x in b if x != 'r']
print a
c[:] = [x for x in c if x != 'r']
print a
print id(a), id(b), id(c)

If you want to learn more about generator expressions, read about generators and the iterator protocol in the python documentation, but you can postpone this a little if you are new to python.

Thanks for the quick reply, I really appreciate it.

Hi everyone!, I had this confusion about the the Bucket Sort Algorithm.
I just wanna know why mostly bucket sort use the range of element in between 0 and 1 ...