0

Here is my code. I am expecting the length of each word in the sentence to be assigned to the list "AS INTEGERS" (not as string), but getting the error as in the subject. Please, help!!

original_str = "The quick brown rhino jumped over the extremely lazy fox"

i = 0
num_words_list = []
original_strSplit = original_str.split(" ")

for each in original_strSplit:
   num_words_list[i:] = len(each)
   i += 1
   print(num_words_list)
Shawn
  • 1,232
  • 1
  • 14
  • 44
Krish-learn
  • 1
  • 1
  • 1

2 Answers2

2

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.

Andrew Merrill
  • 1,672
  • 11
  • 14
  • Just to add on that: the `[x:]` syntax in Python means that the index will count `x` from the end of the list. https://stackoverflow.com/a/4012395/6080254 – Bruno Monteiro Jan 28 '20 at 04:07
  • 2
    I don't think the above comment is quite correct. `[x:]` where x is nonnegative means from index x to the end (so `[0:]` means from the very beginning of the list to the end). You can use a negative value to count from the end (`[-2:]` grabs the last two elements of the list as a slice). – Platinum Azure Jan 28 '20 at 04:15
0

If you are adding items to a list, you can use the append() function. I think you are trying to take a more complicated approach to a simple problem. In your code, you split a string at each " ". This effectively creates a list with all of the words in the original_str that are separated by " ". You then iterate over that list. For each word in original_strSplit (which is now a list), append the length of that word len(each) by using the append() function. Since the len() function returns an integer, you can just put that value in a list by using append(len(each)).

original_str = "The quick brown rhino jumped over the extremely lazy fox"

num_words_list = []
original_strSplit = original_str.split(" ")

for each in original_strSplit:
   num_words_list.append(len(each))

print(num_words_list)

Output: [3, 5, 5, 5, 6, 4, 3, 9, 4, 3]

gmdev
  • 2,725
  • 2
  • 13
  • 28