Aleksandar Beat
Aleksandar Beat

Reputation: 211

Making nested list with for loop using rows from treeview Python

I want to make nested list with values from rows that i get from treeview. I want to get something like this:

[[ , , ], [ , , ], [ , , ]...]

Number of nested lists should be equal to number of rows in tree view, and each nested list should have 5 items. But my program keeps putting all values from all rows to one nested list, and number of nested lists are equal to number of row. How can I fix that.

ListOnePar=[]
ListTwoPar=[]

for child in tree1.get_children(id1):

    one=round(float(tree1.item(child,"values")[1]),2)
    two=round(float(tree1.item(child,"values")[2]),2)
    tree=round(float(tree1.item(child,"values")[3]),2)
    four=round(float(tree1.item(child,"values")[5]),1)
    five=round(float(tree1.item(child,"values")[6]),1)

    ListTwoPar.append(one)
    ListTwoPar.append(two)
    ListTwoPar.append(tree)
    ListTwoPar.append(four)
    ListTwoPar.append(five)

    ListOnePar.append(ListTwoPar)

print(ListOnePar)

Upvotes: 0

Views: 81

Answers (1)

Brendan Abel
Brendan Abel

Reputation: 37559

You need to stick the second list creation inside the loop; otherwise, it's just going to keep appending to the same list for each row.

for child in tree1.get_children(id1):
    ListTwoPar=[]

Upvotes: 1

Related Questions