CortoMaltese CortoMaltese - 3 months ago 8
Python Question

How to get only one list in python?

i need little help about some lists, so i have this part of code:

get_attributeName = \
soup.find(True, {"class": ["product-attributes", "product-attribute-value"]}).find_all('li')

allDataList = []
for attData, attValues in get_attributeName:
data = [attData, attValues.text]
allDataList.append(data)
print(allDataList)


And result after
print(allDataList)
result is:

[['Year: ', '2013']]
[['Year: ', '2013'], ['Color: ', 'Yellow']]
[['Year: ', '2013'], ['Color: ', 'Yellow'], ['Package: ', '5kg']]
[['Year: ', '2013'], ['Color: ', 'Yellow'], ['Package: ', '5kg'], ['Comment: ', 'Null']]
[['Year: ', '2013'], ['Color: ', 'Yellow'], ['Package: ', '5kg'], ['Comment: ', 'Null'], ['Product: ', 'mushrooms']]
[['Year: ', '2013'], ['Color: ', 'Yellow'], ['Package: ', '5kg'], ['Comment: ', 'Null'], ['Product: ', 'mushrooms'], ['Forest: ', 'NULL']]
[['Year: ', '2013'], ['Color: ', 'Yellow'], ['Package: ', '5kg'], ['Comment: ', 'Null'], ['Product: ', 'mushrooms'], ['Forest: ', 'NULL'], ['Country: ', 'France']]


I need for result only last row with all list inside one list, like this:

[['Year: ', '2013'], ['Color: ', 'Yellow'], ['Package: ', '5kg'], ['Comment: ', 'Null'], ['Product: ', 'mushrooms'], ['Forest: ', 'NULL'], ['Country: ', 'France']]

Answer

Your code is printing the list each iteration, as you build it. Simply don't print each iteration; print only after the for loop has completed (if at all).

get_attributeName = soup.find(True,{"class": ["product-attributes", "product-attribute-value"]}).find_all('li')

allDataList = []        
for attData, attValues in get_attributeName:
    data = [attData, attValues.text]            
    allDataList.append(data)
print(allDataList)