title: Iterate through elements in html tree using BeautifulSoup, and produce an output that maintains the relative position of each element? in Python
body: <p>I have this code that does what I need it to do using Jsoup in Java</p>
  
  <pre><code>Elements htmlTree = doc.body().select("*");
  
      Elements menuElements = new Elements();
  
      for(Element element : htmlTree) {
  
          if(element.hasClass("header")) 
              menuElements.add(element);
          if(element.hasClass("name"))
              menuElements.add(element);
          if(element.hasClass("quantity"))
              menuElements.add(element);
      }
  </code></pre>
  
  <p>I want to do the same thing but in Python using BeautifulSoup. An example tree of the HTML I'm trying to scrape follows:</p>
  
  <pre><code>&lt;div class="header"&gt; content &lt;/div&gt;
       &lt;div class="name"&gt; content &lt;/div&gt;
       &lt;div class="quantity"&gt; content &lt;/div&gt;
       &lt;div class="name"&gt; content &lt;/div&gt;
       &lt;div class="quantity"&gt; content &lt;/div&gt;
  &lt;div class="header"&gt; content2 &lt;/div&gt;
       &lt;div class="name"&gt; content2 &lt;/div&gt;
       &lt;div class="quantity"&gt; content2 &lt;/div&gt;
       &lt;div class="name"&gt; content2 &lt;/div&gt;
       &lt;div class="quantity"&gt; content2 &lt;/div&gt;
  </code></pre>
  
  <p>etc.</p>
  
  <p>Basically I want the output to preserve the relative positions of each element. How would I got about doing that using Python and BeautifulSoup?</p>
  
  <p>EDIT: </p>
  
  <p>This is the python code I have (it's very naive) but maybe it can help?</p>
  
  <pre><code>output = []
  
  for e in soup :
    if e["class"] == "pickmenucolmenucat" :
      output.append(e)
    if e["class"] == "pickmenucoldispname" :
      output.append(e)
    if e["class"] == "pickmenucolportions" :
      output.append(e)
  </code></pre>