Search code examples
pythonoptimizationtkinterlistbox

Can I add a list of strings to a Tkinter Listbox without using a loop?


I'm trying to understand what the fastest way is to add a bunch of strings that are pre-arranged in some fashion to a Listbox widget, so that every string is in a new line.

The quickest I could gather so far:

 from Tkinter import *

 strings= 'str1', 'str2', 'str3'
 listbox=Listbox(None)
 [listbox.insert(END, item) for item in strings]
 listbox.pack()

Is there perhaps a cleaner faster way to get it done, without iterating over every string? Perhaps if the strings are pre-packed in a certain way or using some other method?

If it is of relevance, I want to use it to display directory listings.


Solution

  • This code inserts all strings in the collection:

    listbox.insert(END, *strings)