[Tutor] Python program to remove first four even numbers from a list

Manprit Singh manpritsinghece at gmail.com
Sat Oct 16 13:06:05 EDT 2021


Although increment in evencount can be done in else clause

On Sat, 16 Oct, 2021, 22:25 Manprit Singh, <manpritsinghece at gmail.com>
wrote:

> Dear Sir ,
>
> Consider a program that is written to remove first four even numbers from
> a list:
> def remove4even(seq):
>     evencnt = 0
>     lx = []
>     for ele in seq:
>         if evencnt > 3 or ele%2 != 0:
>             lx.append(ele)
>         if ele%2==0:
>             evencnt += 1
>     return lx
>
> lst =[3, 6, 7, 5, 4, 7, 8, 9, 1, 8, 2, 5, 4]
> ans = remove4even(lst)
> print(ans) gives
>
> [3, 7, 5, 7, 9, 1, 2, 5, 4]  that is the right answer
>
> In this program i have used multiple if statements inside for loop. My
>
> question is placing multiple if statements inside a for loop is a right
>
> approach as i have adopted in this example?
>
> Secondarily this example can be done with list comprehensions ?
>
> Regards
>
> Manprit Singh
>
>


More information about the Tutor mailing list