[Tutor] Function that returns True if a list contains odd number else return False

Alan Gauld alan.gauld at yahoo.co.uk
Thu Mar 10 12:40:54 EST 2022


On 10/03/2022 17:14, Manprit Singh wrote:

> Above problem can be solved in two way :
> 
> Inside a  user defined function just returning
> 1) any(1 for ele in seq if ele%2)
> 
> 2)  any(ele%2  for ele in seq)

Intuitively, number 2 should be slightly faster since it
doesn't have an extra test/assignment but in practice
you can only tell by measuring them.

Either of them would be acceptable but personally
I'd favour the second since it expresses the intent
more directly.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list