Search code examples
pythonlistmax

max value of list without max() method


I need to find the max value of a list but I can't use the max() method. How would I go about doing this? I'm just a beginner so I'm sure this is pretty easy to find a workaround, however I'm stumped.

Edit: Forgot to mention that it's python I'm working in.


Solution

  • max = list[0]
    for x in list:
        if x > max:
            max = x
    print max
    

    In this example, we initialize the max value to the first element. Then we iterate through the list, and if we find a larger value than the current max, we assign that value to max. At the end, we should have the largest value, which we print.