Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, what's the method for returning only the max odd integer in a list?

Tags:

python

list

If you have a list of numbers, how do you return only the max odd integer without using the max() function?

I'm assuming it will have something to do with int % 2 != 0, but I'm not sure what else.


I also have to return the overall max integer without using max(), but I got around that by sorting my list and using list[-1].

like image 375
user1186420 Avatar asked Dec 18 '25 23:12

user1186420


1 Answers

max(i for i in my_list if i % 2)

Edit: without max():

def highest_odd(seq):
    """
    Return the highest odd number in `seq`.
    If there are no odd numbers, then return `None`.

    """
    for i in sorted(seq, reverse=True):
        if i % 2:
            return i
like image 103
Jakub Roztocil Avatar answered Dec 21 '25 11:12

Jakub Roztocil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!