QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Find Maximum Difference in List

Finds the maximum difference between any two elements in a list.

  • list
  • difference

Code

def max_difference(lst):
    if not lst or len(lst) < 2:
        return 0
    return max(lst) - min(lst)

Usage

data = [10, 3, 5, 20, 7]
max_difference(data) # Returns: 17