title: ElementTree, .set() and iteration
body: <p>This is my first post on Stack Overflow and am a novice programmer.</p>
  <p>I am having trouble using ElementTree and the .set() method. Using an f-string I am able to assign recipe_id with the correct number.</p>
  <p>When I try to set the recipe_name attribute, it returns only the last element in the name_list array. I'm a bit lost! I'm sure it's something in my syntax or just my understanding of how I'm actually iterating through the items...I just don't understand because the recipe_id portion works just fine.</p>
  <p>Expected output (within the XML)</p>
  <pre><code>&lt;recipes recipe_id=&quot;1&quot; recipe_name=&quot;Apples&quot;&gt;
  &lt;recipes recipe_id=&quot;2&quot; recipe_name=&quot;Oranges&quot;&gt;
  </code></pre>
  <p>Instead I get:</p>
  <pre><code>&lt;recipes recipe_id=&quot;1&quot; recipe_name=&quot;Oranges&quot;&gt;
  &lt;recipes recipe_id=&quot;2&quot; recipe_name=&quot;Oranges&quot;&gt;
  
  </code></pre>
  <p>My code:</p>
  <pre><code>#!/usr/bin/env python3
  
  import os
  import os.path 
  import xml.etree.ElementTree as ET
  
  filename = &quot;my_recipes.xml&quot;
  xmlTree = ET.parse(filename)
  root = xmlTree.getroot()
  
  #change the recipe id in recipes 
  i = 0 
  for element in root.iter(&quot;recipes&quot;):
      i += 1
      element.set('recipe_id', f&quot;{i}&quot;)
   
   #for every tbody in the tree
  name_list = []
  for tbody in root.iter(&quot;tbody&quot;):
      #for every time you find a row 
      for row in tbody.findall('row'):
          data = row.find('entry').text
          #get those rows attribs
          rec_name = row.find('entry').attrib
          #if the row is the row that i want (contains the recipe name)...i couldn't figure out a better way to get this value precisely
          if rec_name == {'namest': 'c1', 'nameend': 'c2', 'align': 'left', 'valign': 'bottom'}:
              #yoink name and stick it in name_list
              name_list.append(data)
   
  for recipes in root.findall('recipes'):
      for i in range(len(name_list)):
          recipes.set('recipe_name', F&quot;{name_list[i]}&quot;)
   
  xmlTree.write(filename, encoding='UTF-8', xml_declaration=True)
  </code></pre>
  <p>My XML:</p>
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;Root&gt;
      &lt;recipes&gt;
          &lt;tbody&gt;
              &lt;row&gt;
                  &lt;entry namest=&quot;c1&quot; nameend=&quot;c2&quot; align=&quot;left&quot; valign=&quot;bottom&quot;&gt;Apples&lt;/entry&gt;
              &lt;/row&gt;
          &lt;/tbody&gt;
      &lt;/recipes&gt;
      &lt;recipes&gt;
          &lt;tbody&gt;
              &lt;row&gt;
                  &lt;entry namest=&quot;c1&quot; nameend=&quot;c2&quot; align=&quot;left&quot; valign=&quot;bottom&quot;&gt;Oranges&lt;/entry&gt;
              &lt;/row&gt;
          &lt;/tbody&gt;
      &lt;/recipes&gt;
  &lt;/Root&gt;
  </code></pre>
  <p>FIXED:
  The code should be</p>
  <pre><code>
  for i,recipes in enumerate(root.findall('recipes')):
      recipes.set('recipe_name',name_list[i])
  </code></pre>
  <p>and I am just stupid.</p>