Skip to content

Instantly share code, notes, and snippets.

@cibofdevs
Last active September 19, 2022 13:09
Show Gist options
  • Save cibofdevs/45ce3c620da9714379044dbdce441744 to your computer and use it in GitHub Desktop.
Save cibofdevs/45ce3c620da9714379044dbdce441744 to your computer and use it in GitHub Desktop.
# 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"
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)
@blind0x
Copy link

blind0x commented Oct 27, 2020

Simplest:

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

@mattmaciel
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment