Odd list behavior

norseman norseman at hughes.net
Thu May 14 18:49:33 CEST 2009

Rhodri James wrote:
> On Wed, 13 May 2009 23:08:26 +0100, norseman <norseman at hughes.net> wrote:
>> Evan Kroske wrote:
>>> I'm working on a simple file processing utility, and I encountered a 
>>> weird error. If I try to get the first element of a list I'm 
>>> splitting from a string, I get an error:
>>>  key = string.split()[0]
>>> Error!
>>>  However, I can slice the list like normal, but that gives me a 
>>> one-element-long list:
>>>  key = string.split()[:1]
>>> Success!
>>>  Finally, the operation works perfectly if I initialize the list 
>>> beforehand:
>>>  list = string.split()
>>> key = list[0]
>>> Success!
>>>  Why does this happen?
>> ==========================
>> Take a look at the  split() command.
>> I think you will find you need one var on the left side for each piece 
>> on the right.
> Given that he's immediately indexing the split results, that's irrelevant.
> There's no point in even guessing with out the traceback.

"...the first element of a list..."
"key = string.split()[0]"
if the list is a list of tuples the error message is correct.
same for a list of key:value pairs
he will need one var on left for each piece of [0]
  k,v=  list.split(key:value)    ??!!


More information about the Python-list mailing list