[Tutor] creating variable names from string slices

alan.gauld@bt.com alan.gauld@bt.com
Tue Apr 1 08:07:02 2003


> Betty     Blonde    Student
> Veronica  Brunette  Student
> 
> What I want to do is define a function that gets passed each line, and
> returns a dictionary with the first element  as the name, and 
> the subsequent elements as the key:value pairs:

Split the line then assign the firstvelement as the key and the rest as the
value.
Something like:

words = line.split()
data[words[0]] = words[1:]

> How do I make this trick work?

Split the string rather than using slicing. You probably want to strip() 
any whitespace off the words before inserting them too...

Alan g.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld/