Concatenating strings from two different for loops at each turn
<p>I have two different <code>for</code> loops that run the same number of times and produce a string at each iteration. (I am scraping an html file) I want the string from first loop to merge/concatenate/append with the string from the second loop FOR EACH ITERATION ( this is the tricky part )Here is the code i have:</p>

<pre><code>from bs4 import BeautifulSoup

bsObj = BeautifulSoup(open("samfull.html"), "html.parser")
tableList = bsObj.find_all("table", {"class":"width100 menu_header_top_emr"}) 
tdList = bsObj.find_all("td", {"class":"menu_header width100"})

for table in tableList:
    first_part_of_row_string = ''
    item = table.find_all("span", {"class":"results_body_text"})
    for i in range(len(item)):
        first_part_of_row_string += (item[i].get_text().strip() + ", ")

for td in tdList:
    second_part_of_row_string = ''
    items = td.find_all("span", {"class":"results_body_text"})
    for i in range(len(items)):
        second_part_of_row_string += (items[i].get_text().strip() + ", ")
</code></pre>

<p>To give an example: </p>

<p>Sample results for the <code>for table in tableList</code> loop are:</p>

<pre><code>a,b,
1,2,
father, mother,
</code></pre>

<p>and for the <code>for td in tdList</code> loop are:</p>

<pre><code>c, d, e,
3, 4, 5,
son, daughter, twin,
</code></pre>

<p>I want to combine the <code>first_part_of_row_string</code> of each iteration with the <code>second_part_of_row_string</code> of each iteration as well</p>

<p>so I want to print out this:</p>

<pre><code>a, b, c, d, e,
1, 2, 3, 4, 5
father, mother, son, daughter, twin,
</code></pre>

<p>which are effectively <code>first_part_of_row_string + second_part_of_row_string</code> of each iteration of both loops</p>

<p>The length of tableList and tdList is the same so both loops will always return the same number of rows. I could have in one loop if the td was in the same table that is being referred to in the tableList, unfortunately it is not. In the html the table with the class specified in tableList definition is always followed by another table that has no class but always contains a td with the class specified in tdList. A sample occurrence of this html is included below. The whole page is some thousands of line, so I am putting it on a seperate link.<a href="http://pastebin.com/N0qih4Hj" rel="nofollow">link</a></p>

<pre><code>&lt;table cellspacing="0" cellpadding="0"
        style="margin-left: auto; margin-right: auto;" class="width100 menu_header_top_emr"&gt;
        &lt;tbody&gt;
            &lt;tr&gt;
                &lt;td style="width:80px;"&gt;
                    &lt;div style="width:70px;background-color:#B2EE98; border:1px solid grey; padding:2px 5px 2px 5px; text-align:center;"&gt;Entity&lt;/div&gt;
                &lt;/td&gt;
                &lt;td style="padding-left:5px;"&gt;
                    &lt;span class="results_body_text"&gt;&lt;h5 style="vertical-align: middle;"&gt;Rascal X-Press, Inc.&lt;/h5&gt;&lt;/span&gt;
                &lt;/td&gt;
                &lt;td style="width:130px;"&gt;
                    &lt;div class="right"&gt;
                    &lt;span class="results_title_text"&gt;Status:&lt;/span&gt; 
                    &lt;span class="results_body_text"&gt;
                        Submitted
                    &lt;/span&gt;
                    &lt;/div&gt;
                &lt;/td&gt;
                &lt;td style="width:22px;"&gt;
                    &lt;a href="" class="more_duns_link_emr right" style="display: inline;"&gt;&lt;img
                        id="more_duns_link_emr"
                        src="/SAMSearch/styles/img/expand-small-blue.png" style="padding:8px 8px 8px 2px;" 
                        alt="Expand Search Result for Rascal X-Press, Inc."&gt;&lt;/a&gt;
                    &lt;a href="" class="hide_duns_link_emr off right" style="display: none;"&gt;&lt;img
                        id="hide_duns_link_emr"
                        src="/SAMSearch/styles/img/collapse-small-blue.png" style="padding:8px 8px 8px 2px;" 
                        alt="Collapse Search Result for Rascal X-Press, Inc."&gt;&lt;/a&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;    
    &lt;table&gt;
        &lt;tbody&gt;
            &lt;tr&gt;
                &lt;td class="menu_header width100"&gt;
                    &lt;table&gt;
                        &lt;tr&gt;
                            &lt;td style="width:25%;"&gt;
                                &lt;span class="results_title_text"&gt;DUNS:&lt;/span&gt; &lt;span class="results_body_text"&gt; 012361296&lt;/span&gt;
                            &lt;/td&gt;                                                       
                            &lt;td style="width:25%;"&gt;
                            &lt;/td&gt;

                            &lt;!-- label as CAGE when US Territory is listed as Country --&gt;
                            &lt;td style="width:27%;"&gt;
                                    &lt;span class="results_title_text"&gt;CAGE Code:&lt;/span&gt; &lt;span class="results_body_text"&gt;&lt;/span&gt;

                            &lt;/td&gt;
                            &lt;td style="width:15%" rowspan="2"&gt;
                                &lt;input type="button" value="View Details" title="View Details for Rascal X-Press, Inc." class="center" style="height:25px; width:90px; vertical-align:middle; margin:7px 3px 7px 3px;" onClick="viewEntry('4420848', '1472652382619')" /&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td colspan="2"&gt;
                                &lt;span class="results_title_text"&gt;Has Active Exclusion?: &lt;/span&gt;
                                &lt;span class="results_body_text"&gt;
                                    No
                                &lt;/span&gt;
                            &lt;/td&gt;
                            &lt;td&gt;
                                &lt;span class="results_title_text"&gt;DoDAAC:&lt;/span&gt; &lt;span class="results_body_text"&gt;&lt;/span&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td colspan="2"&gt;
                                &lt;span class="results_title_text"&gt;Expiration Date:&lt;/span&gt;
                                &lt;span class="results_body_text"&gt;
                                &lt;/span&gt;
                            &lt;/td&gt;
                            &lt;td colspan="2"&gt;&lt;span class="results_title_text"&gt;Delinquent Federal Debt?&lt;/span&gt;
                                &lt;span class="results_body_text"&gt;
                                        No 
                                &lt;/span&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;tr&gt;
                            &lt;td colspan="2"&gt;&lt;span class="results_title_text"&gt;Purpose of Registration:&lt;/span&gt;
                                &lt;span class="results_body_text"&gt;
                                    Federal Assistance Awards Only
                                &lt;/span&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;/table&gt;
                    &lt;div class="off_duns_emr" style="display: none;"&gt;
                        &lt;table class="resultbox1 menu_header width100"
                            style="margin-left: auto; margin-right: auto;" cellpadding="2"&gt;
                            &lt;tbody&gt;
                                &lt;tr&gt;
                                    &lt;td colspan="3"&gt;&lt;span class="results_title_text"&gt;Address:&lt;/span&gt;
                                    &lt;span class="results_body_text"&gt;1372 State Hwy 37&lt;/span&gt;&lt;/td&gt;
                                &lt;/tr&gt;

                                &lt;tr&gt;
                                    &lt;td style="width:212px;"&gt;&lt;span class="results_title_text"&gt;City:&lt;/span&gt;
                                    &lt;span class="results_body_text"&gt;West Frankfort&lt;/span&gt;&lt;/td&gt;

                                    &lt;td style="width:200px;"&gt;&lt;span class="results_title_text"&gt;State/Province:&lt;/span&gt;
                                    &lt;span class="results_body_text"&gt;IL&lt;/span&gt;&lt;/td&gt;
                                &lt;/tr&gt;
                                &lt;tr&gt;
                                    &lt;td style="width:130px;"&gt;&lt;span class="results_title_text"&gt;ZIP Code:&lt;/span&gt;
                                    &lt;span class="results_body_text"&gt;62896-5007&lt;/span&gt;&lt;/td&gt;

                                    &lt;td style="width:200px;"&gt;&lt;span class="results_title_text"&gt;Country:&lt;/span&gt;
                                    &lt;span class="results_body_text"&gt;UNITED STATES&lt;/span&gt;&lt;/td&gt;
                                &lt;/tr&gt;
                            &lt;/tbody&gt;
                        &lt;/table&gt;
                    &lt;/div&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;&lt;/td&gt;
            &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;                                &lt;/li&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;/table&gt;
</code></pre>

<p>There are a lots of ways to do what you're asking for, here's a very simple one:</p>

<pre><code>tableList = [
    ["a", "b"],
    ["1", "2"],
    ["father", "mother"]
]

tdList = [
    ["c", "d", "e"],
    ["3", "4", "5"],
    ["son", "daughter", "twin"]
]

len_list = max(len(tableList), len(tdList))

for i in range(len_list):
    print ", ".join(tableList[i] + tdList[i])
</code></pre>

python string python-2.7 loops concatenation