[Tutor] populating a listbox from a list

jfouhy@paradise.net.nz jfouhy at paradise.net.nz
Sat Jul 16 01:14:58 CEST 2005

Quoting Max Russell <max_russell2000 at yahoo.co.uk>:

> I have a list in this format:
> puckman puckmana puckmanf puckmanh pacman 
> pacmanf puckmod pacmod 
> newpuc2 newpuc2b newpuckx pacheart hangly 


You can use .split() to turn a string like that into a list of the individual
(when called with no arguments, .split() splits on all whitespace)

>>> s = """foo bar baz
... zif zaf zof
... yip yap"""
>>> s
'foo bar baz\nzif zaf zof\nyip yap'
>>> s.split()
['foo', 'bar', 'baz', 'zif', 'zaf', 'zof', 'yip', 'yap']

> I'm really not sure how to populate the list though- I
> can work with lists just as text, but what do I need
> to do to read a list into a listbox.

To insert into a listbox, you use self.lstbx.insert(position, *entries)

For example:

for elem in s.split():
 self.lstbx.insert(END, elem)

You can also do this:

self.lstbx.insert(END, *s.split())

(if x is a list and foo() a function, then foo(*x) is equivalent to foo(x[0],
x[1], x[2], ..., x[len(x)-1]))


More information about the Tutor mailing list