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

Alan Gauld learn2program at gmail.com
Sat Oct 16 18:52:08 EDT 2021


On 16/10/2021 17:55, Manprit Singh wrote:
> lst =[3, 6, 7, 5, 4, 7, 8, 9, 1, 8, 2, 5, 4]

> Secondarily this example can be done with list comprehensions ?

Just for laughs...

>>> count = 0
>>> [n for n in lst if n%2 or ((count := count+1) > 4)]
[3, 7, 5, 7, 9, 1, 2, 5, 4]

But I probably wouldn't recommend it. Explicit is better in this case I
think.

Alan G




More information about the Tutor mailing list