title: How to find all local maxima and minima in a list
body: <p>I have a list 2,3,4,3,5,9,4,5,6
  I want to iterate over the list until I get the first highest number that is followed by a lower number. Then to iterate over the rest of the number until I get the lowest number followed by a higher number. Then the next highest highest number that is followed by a lower number.And so on.The result I want is 2,4,3,9,4,6
  Here is my last attempt.I seem to be going round in circles</p>
  
  <pre><code>#!/usr/bin/env python
  value = []
  high_hold = [0]
  low_hold = [20]
  num = [4,5,20,9,8,6,2,3,5,10,2,]
  
  def high():
      for i in num:
          if i &gt; high_hold[-1]:
              high_hold.append(i)
  
  def low():
      for i in num:
          if i &lt; low_hold[-1]:
              low_hold.append(i)
  
  high()
  a = high_hold[-1]
  value.append(a)
  high_hold = high_hold[1:]
  b = len(high_hold) -1
  num = num[b:]
  
  low()
  c = len(low_hold) -1
  num = num[c:]
  value.append(b)
  print('5:  ', value, '(this is what we want)')
  print(num)
  
  high_hold = [0]
  
  def high():
      for i in num:
          if i &gt; high_hold[-1]:
             high_hold.append(i)
  
  high()
  a = high_hold[-1]
  print(a)
  print('1:  ', high_hold, 'high hold')
  </code></pre>