Print largest/smallest no (even, odd) from user defined list of given size After reviews of my previous codes 1st, 2nd learned many new things. So with those in mind, this is my solution for another question.

Question: Write a program that ask user for list size and values and find largest and smallest no, even no, odd no?
Expected output: For user defined list,

[13, 7, 19, 21, 3, 4, 27, 47, 50, 11] -&gt; 50, 3, 50, 4, 47, 3

[2, 4, 6, 8, 10] -&gt; 10, 2, 10, 2, None, None

[1, 3, 5, 7, 9] -&gt; 9, 1, None, None, 9, 1


Format: largest_no, smallest_no, largest_even, smallest_even, largest_odd, smallest_odd

My solution:
def largest_and_smallest(given_list):
    &quot;&quot;&quot;Extracts required numbers from given_list.

    &gt;&gt;&gt; largest_and_smallest([13, 7, 19, 21, 3, 4, 27, 47, 50, 11])
    (50, 3, 50, 4, 47, 3)

    &gt;&gt;&gt; largest_and_smallest([2, 4, 6, 8, 10])
    (10, 2, 10, 2, None, None)

    &gt;&gt;&gt; largest_and_smallest([1, 3, 5, 7, 9])
    (9, 1, None, None, 9, 1)

    &quot;&quot;&quot;
    sorted_list = sorted(given_list)
    largest_no = max(sorted_list)
    smallest_no = min(sorted_list)

    list_of_even = [even_no for even_no in sorted_list if ((even_no % 2) == 0)]
    if len(list_of_even) == 0:
        largest_even = None
        smallest_even = None
    else:
        largest_even = max(list_of_even)
        smallest_even = min(list_of_even)

    list_of_odd = [odd_no for odd_no in sorted_list if ((odd_no % 2) != 0)]
    if len(list_of_odd) == 0:
        largest_odd = None
        smallest_odd = None
    else:
        largest_odd = max(list_of_odd)
        smallest_odd = min(list_of_odd)

    return largest_no, smallest_no, largest_even, smallest_even, largest_odd, smallest_odd


def input_handler():
    &quot;&quot;&quot;Checks validity of input. Any mistakes causes the program to stops.&quot;&quot;&quot;
    size = input(&quot;Enter a size of list?\n&gt;&quot;)
    if size.strip() == &quot;&quot;:
        exit(&quot;Program Terminated. (No Input)&quot;)
    elif size.isdigit():
        list_size = int(size)
        list_of_no = [int(input(&quot;Enter value:&quot;)) for _ in range(list_size)]  # bad input causes error.
        return list_size, list_of_no
    else:
        exit(&quot;Program Terminated. (Bad Input)&quot;)


def main():
    print(&quot;All input should be integers.&quot;)
    list_size, list_of_no = input_handler()
    l_no, s_no, l_even, s_even, l_odd, s_odd = largest_and_smallest(list_of_no)
    print()
    print(f&quot;From provided values of list of a size {list_size}:&quot;)
    print(f&quot;Largest no.: {l_no}, Smallest no.: {s_no}, Largest even no.: {l_even}, &quot;
          f&quot;Smallest even no.: {s_even}, Largest odd no.: {l_odd}, Smallest odd no.: {s_odd}&quot;)
    return


if __name__ == '__main__':
    import doctest
    doctest.testmod(main())

So, from this review I would like to know:

I have used list comprehension as per suggestion in previous code, have I used that correctly? For example, I made separate list for even and odd numbers could I have done that in different way?

I made a separate function to handle input is that a bad practice? What is the best way to handle inputs, that checks different corner cases?


If there is any recommendations going forward from this will help me a lot.
Your variables are long, without needing to be.
You don't have to define largest_even, amongst others, it'd be simpler to just return the tuple.
If you're targeting 3.4+, and I don't see why you wouldn't, you can use the default keyword. "New in version 3.4: The default keyword-only argument."


Leaving you simply with:

def largest_and_smallest(numbers):
    even = [n for n in numbers if (n % 2) == 0]
    odd  = [n for n in numbers if (n % 2) != 0]
    return (
        max(numbers, default=None),
        min(numbers, default=None),
        max(even, default=None),
        min(even, default=None),
        max(odd, default=None),
        min(odd, default=None)
    )


If you don't like the verbosity of max, you could use functools.partial to set default twice.

from functools import partial

def largest_and_smallest(numbers):
    min_ = partial(min, default=None)
    max_ = partial(max, default=None)
    even = [n for n in numbers if (n % 2) == 0]
    odd  = [n for n in numbers if (n % 2) != 0]
    return (
        max_(numbers),
        min_(numbers),
        max_(even),
        min_(even),
        max_(odd),
        min_(odd)
    )