Python 2.2 code continues running before list comprehension is completed?

Chris P. chris.peressotti at
Mon Jul 19 16:25:51 CEST 2004

Hi.  I've made a program that logs onto a telnet server, enters a
command, and then creates a list of useful information out of the
information that is dumped to the screen as a result of the command. 
Here's a generic version of the code in question:


# Prior code opens telnet connection "tn" and logs in.
tn.read_until('> ')
tn.write('THE COMMAND IS HERE\n')
dump = tn.read_very_eager()
dump_lines = dump.split('\n')
dump_info = [x for x in dump_lines if (VARIOUS CONDITIONS ON x)]

# string1 and string2 are strings that are known to
# always appear in the dump.
A = dump_info.index('string1\r')
B = dump_info.index('string2\r')
C = A - B - 1


The thing is, I can step through this code in the debugger and it
works fine.  When run from the command line, however, I get the
following error in response to my dump_info.index lines:

ValueError: list.index(x): x not in list

So I tried putting (for x in dump_info: print x) right after I make
the list comprehension and, when I run that, it shows that the list is
incomplete... so it SEEMS like Python begins to create the list
comprehension and then continues to execute commands before the list
comprehension is finished!

Any help with a solution or workaround will be greatly appreciated,

- Chris

More information about the Python-list mailing list