title: Python: how to find value in list smaller than target
body: <p>For example I have a non-ordered list of values [10, 20, 50, 200, 100, 300, 250, 150]</p>
  
  <p>I have this code which returns the next greater value:</p>
  
  <pre><code>def GetNextHighTemp(self,  temp,  templist):
      target = int(temp)
      list = []
      for t in templist:
          if t != "":
              list.append(int(t))
      return str(min((abs(target - i), i) for i in list)[1])
  </code></pre>
  
  <p>e.g. If temp = 55, it will return '100'.</p>
  
  <p>But how can I get the lesser of the value? That is how to get it to return '50'?</p>
  
  <p>Thank you.</p>
  
  <p><strong>EDIT - now working</strong></p>
  
  <pre><code>def OnTWMatCurrentIndexChanged(self):
      self.ClearTWSelectInputs()
      material = self.cb_TW_mat.currentText()
      temp = self.txt_design_temp.text()
      if material != "":
          Eref = self.GetMaterialData(material,  "25",  "elast")
          if Eref and Eref != "":
              Eref = str(float(Eref) / 1000000000)
              self.txt_TW_Eref.setText(Eref)
          else:
              self.txt_TW_Eref.setText("194.8")
              self.ShowMsg("No temperature match found for E&lt;sub&gt;ref&lt;/sub&gt; in material data file. Value of 194.8 GPa will be used.",  "blue")
      if material != "" and temp != "":
          if self.CheckTWTemp(material,  temp):
              dens = self.GetMaterialData(material,  temp,  "dens")
              self.txt_TW_dens.setText(dens)
              elast = self.GetMaterialData(material,  temp,  "elast")
              elast = str(float(elast) / 1000000000)
              self.txt_TW_Et.setText(elast)
              stress = self.GetMaterialData(material,  temp,  "stress")
              stress = str(float(stress) / 1000000)
              self.txt_TW_stress_limit.setText(stress)
          else:
              self.ShowMsg("No temperature match found for " + temp + "&amp;#x00B0; C in material data file. Extrapolated data will be used where possible or add new material data.",  "blue")
              dens = self.GetExtrapolatedMaterialData(material,  temp,  "dens")
              self.txt_TW_dens.setText(dens)
              elast = self.GetExtrapolatedMaterialData(material,  temp,  "elast")
              elast = str(float(elast) / 1000000000)
              self.txt_TW_Et.setText(elast)
              stress = self.GetExtrapolatedMaterialData(material,  temp,  "stress")
              stress = str(float(stress) / 1000000)
              self.txt_TW_stress_limit.setText(stress)
      else:
          self.ClearTWSelectInputs()
  
  def CheckTWTemp(self, matvar, tempvar):
      for material in self.materials:
          if material.attrib["name"] == matvar:
              temps = material.getiterator("temp")
              for temp in temps:
                  if int(temp.text) == int(tempvar):
                      return True
              return False
  
  def GetMaterialData(self, matvar, tempvar, tag):
      for material in self.materials:
          if material.attrib["name"] == matvar:
              temps = material.getiterator("temp")
              for temp in temps:
                  if temp.text == tempvar:
                      value = temp.find(tag)
                      return value.text
  
  def GetExtrapolatedMaterialData(self, matvar, tempvar, tag):
      try:
          templist = QStringList()
          for material in self.materials:
              if material.attrib["name"] == matvar:
                  temps = material.getiterator("temp")
                  for temp in temps:
                      templist.append(temp.text)
          templist.sort()
          target = int(tempvar)
          x1 = max(int(t) for t in templist if t != '' and int(t) &lt; target)
          x2 = min(int(t) for t in templist if t != '' and int(t) &gt; target)
          y1 = float(self.GetMaterialData(matvar, str(x1), tag))
          y2 = float(self.GetMaterialData(matvar, str(x2), tag))
          x = target
          y = y1 - ((y1 - y2) * (x - x1) / (x2 - x1))
          return str(y)
      except Exception, inst:
          return "0"
  </code></pre>