create a list of word lengths for the words in original_str using the accumulation pattern

Question:

The questions is this:

Write code to create a list of word lengths for the words in original_str using the accumulation pattern and assign the answer to a variable num_words_list. (You should use the len function).

original_str = "The quick brown rhino jumped over the extremely lazy fox".split()
original_list = list(original_str)
num_words = len(original_list)
for i in original_list:
    print(len(i))

This is my output but it needs to be as a list and in the num_words_list variable, but I can’t seem to convert it.

3
5
5
5
6
4
3
9
4
3

Asked By: Lefteris Theodorou

||

Answers:

Without giving too much away:

  1. Create an empty list before your loop
  2. Instead of calling print on each item, add the item to the list.

Both steps should be quite easy to find online.

Good luck!

Answered By: Bart Friederichs

Hi I think this is what you’re looking for?

original_str = "The quick brown rhino jumped over the extremely lazy fox".split()
original_list = list(original_str)
num_words = len(original_list)
num_words_list = []
for i in original_list:
    num_words_list.append((len(i)))

print(num_words_list)
Answered By: Reez0

You could append each integer to an empty list, that way you should be okay 🙂

But a quick search in this general direction will have you figure this out in no time, best of luck!

Answered By: David S
original_str = "The quick brown rhino jumped over the extremely lazy fox"


original_list = list(original_str.split())
num_words = len(original_list)
num_words_list = []
for i in original_list:
    num_words_list.append((len(i)))

print(num_words_list)
Answered By: Ahmed El Zemrany

You could also use split() method, which transforms a string into a list of words.

original_str = "The quick brown rhino jumped over the extremely lazy fox."
split_str = original_str.split()
num_words_list=[]
for words in split_str:
    num_words_list.append(len(words))

Answered By: João Vitor Gomes
original_str = "The quick brown rhino jumped over the extremely lazy fox"
abc = original_str.split(" ")
num_words_list = []
for i in abc:
    b = len(i)
    num_words_list.append(b)
print(num_words_list)
Answered By: Shakil Quraishi

You can do the same just in one line using Python list comprehension:

original_str = "The quick brown rhino jumped over the extremely lazy fox".split()
print([len(word) for word in original_str])
Answered By: Rishab P

Answer:

original_str = "The quick brown rhino jumped over the extremely lazy fox"
original_list = list(original_str.split())
num_words_list = []
for i in original_list:
    num_words_list.append((len(i)))

print(num_words_list)
Answered By: Anirban Saha

First split the string into a list. Then create an empty list, start a for loop and append the length of the words:

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

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

for i in num_words_list1:
    num_words_list.append(len(i))

print(num_words_list)
Answered By: Princy Nadar

You can try the following codes as well

original_str = "The quick brown rhino jumped over the extremely lazy fox"
splt = original_str.split(" ")
print(splt)
count = 0
num_words_list = [] 
for i in splt:
    num_words_list.append(len(i))

print(num_words_list)

The output is

['The', 'quick', 'brown', 'rhino', 'jumped', 'over', 'the', 'extremely', 'lazy', 'fox']
[3, 5, 5, 5, 6, 4, 3, 9, 4, 3]

Hope it helps!

Answered By: mahmoudpd
original_str = "The quick brown rhino jumped over the extremely lazy fox"
num_words_list = []
for num in original_str.split():
    num_words_list += [len(num)]


print(num_words_list)
Answered By: Amrindra
original_str = "The quick brown rhino jumped over the extremely lazy fox"
original_str_list = original_str.split()
num_words_list = []
for word in original_str_list:
    num_words_list.append(len(word))

#1. This is original string

#2. Here, we convert words in string to individual elements in list

#2. [‘The’, ‘quick’, ‘brown’, ‘rhino’, ‘jumped’, ‘over’, ‘the’, ‘extremely’, ‘lazy’, ‘fox’]

#3. Creating empty list to hold length of each word

#4. for loop to iterate over every element (word) in element

#4. for loop working – First it calculates length of every element in list one by one and then appends this length to num_words_list varaible

Answered By: Sahid
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.