All Courses

Why not getting result of a as [4, 7,8,3,2]

By Sudipchoudhury.myapplications, 2 years ago
  • Bookmark
0

8,3,2 are not divisible by 5 then it should have got appended to a. But in the result of a not getting those numbers. Why those numbers 8,3,2 are not reflecting.


l = [4, 7, 5, 10, 20, 50, 50, 8, 3, 2]

a = []

for i in l:

  if (i%5)==0:

    print(i)

    break

  else:

    a.append(i)

print(a)


5
[4, 7]

Python

remove break statement

– balasahebgarule Jul 28, 2021 at 11:02 AM

1 Answer
0
Pritam Shinde

The correct program is:

l = [4, 7, 5, 10, 20, 50, 50, 8, 3, 2]

a = []

for i in l:
  if (i%5)==0:
    print(i)
    #break   - remove break statement 

  else:
    a.append(i)

print(a)

Output is:

5
10
20
50
50
[4, 7, 8, 3, 2]

Your Answer

Webinars

Why You Should Learn Data Science in 2023?

Jun 8th (7:00 PM) 289 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More