The line in question is num_words_list[i:] = len(each)
.
The question is, what are you trying to do on this line?
If you are trying to add a new element on to the end of the list, then perhaps you want to use Python's append
function, which does exactly that.
For example, num_words_list.append(...)
would append the value in place of the ...
onto the end of the list.
If you are trying to replace an existing element of the list, then your line of code is almost correct, but shouldn't include the stray colon. Instead, it should read like this: num_words_list[i] = ...
. However, this won't work in your case because your list doesn't have any elements to replace (it starts out empty, after all).
In Python, you can use a colon inside the brackets when accessing a list, but what that does is create a slice of the list, or a sublist. For example, if you write num_words_list[3:8]
, then it slices the list by cutting it just before element 3 and just before element 8, making a new shorter list that has 5 elements in it (elements 3, 4, 5, 6, and 7) from the original list. Of course, this only makes sense if the list actually has this many elements already in it. You can optionally omit the endpoints of the slice. If you omit the first endpoint, the slice starts at the beginning of the list, and if you omit the second endpoint, then the slice ends at the end of the list. So, you were unintentionally creating a slice that extended from just before element i
and went to the end of the list. You were also trying to assign to this slice, replacing it with a different list, but the value you were replacing it with (len(each)
) was not a list, resulting in the TypeError
that you saw.