• + 1 comment

    You can do so with Counter from collections in order to take care of mode with standard libs while avoiding the StatisticsError potential of being raised that comes from statistics version of mode:

    from collections import Counter
    
    numbers = [ 1, 1, 2, 2, 3, 3, 4, 1, 2 ]
    mode = Counter(sorted(numbers)).most_common(1)[0][0]
    

    EDIT: Actually, this can be done without collections easily too:

    numbers = [ 1, 1, 2, 2, 3, 3, 4, 1, 2 ]
    mode = max(sorted(numbers), key=numbers.count)
    

    sorted(numbers) is used to ensure that the lowest value most-common number is selected.