+ 2
for loops
So I completed another code coach, this time the average word length problem. In it, I had to use a for loop, which confused me a bit. Previously, if I had a loop that started with for i in list: it ended with something like i=i+1 but this time I only had to do for word in indiv_words: word_len.append(len(word)) how does the for loop continue to the next item in the list without something like word=word+1? https://code.sololearn.com/cy16AZcG962r/?ref=app
4 ответов
+ 8
The for loop does not need to get an increment to work, it just iterates through a iterable like a list, as long as there are elements. May be the increment you mentioned (i = i+1) was used as a counter, that does not affect the loop. Can you post this code also? Thanks!
+ 2
sorry, which code are you asking for? Also thanks for the thorough answers both of you! Loving this community more and more all the time :D
+ 1
It's a so called "for-each-loop" which automatically iterates over all items from a given iterable (list in this case).
+ 1
on a side note, appending things to an empty list is an "impure function" right?