How to find the maximum by comparing previous value?
<p>I have a huge data set &amp; complex code that it takes so much time if I am trying to find the maximum by appending all intermediate results and compare. So I want to implement algorithm to find the maximum by comparing previous value.
my algorithm goes like, </p>

<pre><code>for i in range(len(y)):
  oldmax = y[0]
  if oldmax &gt;= y[i]:
    pass
  else:
    new_max = y[i]
</code></pre>

<p>then I want to store this newmax and compare with next string y[i+1] and go on (Only the maximum value should survive at the end).
But I am not sure how to set this new_max to be something to be compared in the next loop. For example, let y = [3,1,5,6,4]. Since the y[0] is greater than y[1], it will pass until it meets 5. But since 6 and 4 are also greater than 3, the new_max ends up being 4 which is the last value.</p>

<p>How should I fix the code? Any help will be appreciated!</p>

<p><strong><em>If</em></strong> you want to do it like that, you don't need to work with <code>index</code>, which will probably be slightly slower on huge data. </p>

<p>You can simply do:</p>

<pre><code>newmax = y[0]
for new in y:
    newmax = new if new &gt; newmax else newmax
</code></pre>

<p>You'd need to do a test on huge data though to see if this is fast enough.</p>

python arrays database max