appending to python lists
<p>I have a web scrapper that returns me values like the example below. </p>

<pre><code># Other code above here.
test = []

results = driver.find_elements_by_css_selector("li.result_content")
for result in results:
    # Other code about result.find_element_by_blah_blah
    product_feature = result.find_element_by_class_name("prod-feature-icon")

    for each_item in product_feature.find_elements_by_tag_name('img'):
        zz = test.append(each_item.get_attribute('src')[34:-4])  # returning the values I want
        print(zz)
</code></pre>

<p>The code above would print out the results like this: (Which is the values I want)</p>

<pre><code>TCP_active
CI
DOH_active
TCP_active
CI
DOH
TCP
CI_active
DOH_active
</code></pre>

<p>I want to achieve the results below:</p>

<pre><code>[TCP_active, CI, DOH_active]
[TCP_active, CI, DOH]
[TCP, CI_active, DOH_active]
</code></pre>

<p>how should I be doing it?</p>

<p>I tried:</p>

<pre><code>test.append(each_item.get_attribute('src')[34:-4])
</code></pre>

<p>But this gives me:</p>

<pre><code>[TCP_active]
[TCP_active, CI]
[TCP_active, CI, DOH_active]
[TCP_active, CI, DOH_active, TCP]
...
</code></pre>

<p>Hope my explanation is clear </p>

<p>Rather than <code>print</code>, append your results to lists; one new list per iteration of the outer loop:</p>

<pre><code>test = []

results = driver.find_elements_by_css_selector("li.result_content")
for result in results:
    # Other code about result.find_element_by_blah_blah
    product_feature = result.find_element_by_class_name("prod-feature-icon")
    features = [] 
    for each_item in product_feature.find_elements_by_tag_name('img'):
        features.append(each_item.get_attribute('src')[34:-4])
    test.append(features)
</code></pre>

<p>You can print <code>features</code> if you want to, or <code>test</code>, just to see what is happening at each level of your <code>for</code> loops.</p>

python list