Question about remove ... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Question about remove ...

The result for the operation s.remove(x) is removing the first item of s where s[i] is equal to x. That seems pretty straight forward, however the statement s[1:].remove(x) does not appear to remove x from s[1:]. If s1 is assigned to s[1:] then the operation s1.remove(x) works as expected. I am trying to understand why remove does not work on the first statement? Thank you for any help.

2nd Jan 2020, 12:15 AM
GeoK68
GeoK68 - avatar
1 Answer
+ 6
Because s and s[:] are different. s[:] is just a copy, and not the original list When you use s.remove(x), you're removing from the list itself When you use s[:], you're modifying a copy of that list, and not the list itself. In order to modify the new copy, you have to assign it to variable. Just line s[:], s[1:] is just anither copy of s, that doesn't contain the first element.
2nd Jan 2020, 12:20 AM
Aymane Boukrouh
Aymane Boukrouh - avatar