I'm reading the "Fluent Python" book and a section talks about assigning to slices. For example:
l = list(range(10)) l[2:5] = [20, 30] #l is [0, 1, 20, 30, 5, 6, 7, 8, 9] I tried testing this feature with some customised examples, but the following gives me an error:
l = list(range(10)) l[::-1] = [10, 1] #ValueError: attempt to assign sequence of size 2 to extended slice of size 10 print(l) but this works:
l = list(range(10)) l = l[::-1] l[:] = [10, 1] print(l) #[10, 1] Why am I getting the error? Isn't what I'm trying to do the same as the last cell?
Thanks
lwas (re)defined using a negative step size is irrelevant to the subsequent assignment.