13th Apr 2021, 2:32 PM
Rakesh Verma
Rakesh Verma - avatar
2 Answers
+ 2
As I understand, the expected output is the list of words that are greater than or equal to 5. Then: Solution: You need to create a copy of the original list and remove items from the copy list, meanwhile iterate through the original list (will also work backwards). Tip: it could be made using list constructor (`list (collection)`) or `copy (object)` function from `copy` module. Note: it is good for copying one-level structures (or structures that hold immutable structures); to copy all levels, use copy.deepcopy. Explanation: Your current program does that: word = line[0] = 'The', line[0] is deleted; word = line[1] = 'brown' (as 'quick' is line[0] now); word = line[2] = 'fox', line[2] is deleted; word = line[3] = 'over' (as 'jumps' is line[2] now), line[3] is deleted; word = line[4] = 'lazy' (as 'the' is line[3] now), line[4] is deleted; len(line) == 5, so stop iteration; line after the iteration: ['quick', 'brown', 'jumps', 'the', 'dog']. Recources: https://docs.python.org/3/library/copy.html
13th Apr 2021, 3:16 PM
#0009e7 [get]
#0009e7 [get] - avatar
+ 1
Thank you. I got it
13th Apr 2021, 3:21 PM
Rakesh Verma
Rakesh Verma - avatar