Odd list behavior

Chris Rebert clp2 at rebertia.com
Wed May 13 18:05:10 EDT 2009


On Wed, May 13, 2009 at 2:51 PM, Evan Kroske <e.kroske at gmail.com> 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?

Please include the actual error messages and Tracebacks in the future.
"Error!" and "Success!" aren't much to go on; and answering your
question without further information is just a guessing game at best.

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list