Search code examples
pythonlisttkintertreeview

Split a nested list into separate lists and insert them into a treeview


I have 2 lists

the_nested_list=[[31.753, 68.245, 0.002, 3.791, 50.723], [28.753, 71.245, 0.002, 3.977, 53.169], [27.622, 72.376, 0.002, 4.032, 52.682]]

and a=[a,b,c]

I want to populate a treeview with 3 rows. in each row, the 1st column should be a and b and c. To get a table looks like this

enter image description here

How do I achieve this in tkinter? I have tried datas = np.append(a,the_nested_list) and tree.insert('', tk.END, values=(datas.tolist())) but it's not correctly displaying the values inside the treeview.


Solution

  • this might be easier to understand.

    # Your data
    the_nested_list=[[31.753, 68.245, 0.002, 3.791, 50.723],
     [28.753, 71.245, 0.002, 3.977, 53.169],
     [27.622, 72.376, 0.002, 4.032, 52.682]]
    rows =['a','b','c']
    
    # adding data to the treeview
    for contact,r in zip(the_nested_list,rows):
        tree.insert('', tk.END, values=list(r)+contact)