[issue46302] IndexError inside list comprehension + workaround

2022-01-08 Thread Pierre Fortin
Pierre Fortin added the comment: [Thanks for the replies! I was trying to post this before seeing them.] Major egg on face... The more complex the code becomes, the more likely you will be burned by a rookie mistake... var = '' var[0] WILL give IndexError -- Duh! It was buried in the

[issue46302] IndexError inside list comprehension + workaround

2022-01-07 Thread Steven D'Aprano
Steven D'Aprano added the comment: Your functions test1 and test2 are irrelevant to the bug report. The driver code using eval() to pick which function to call is unneeded. The business of simulating a file is complexity for no purpose. Those ignore, count, unique functions are also

[issue46302] IndexError inside list comprehension + workaround

2022-01-07 Thread Stefan Pochmann
Stefan Pochmann added the comment: The error occurs when you do code.strip()[0] when code is " ", not "u2". -- nosy: +Stefan Pochmann ___ Python tracker ___

[issue46302] IndexError inside list comprehension + workaround

2022-01-07 Thread Steven D'Aprano
Steven D'Aprano added the comment: > it returns IndexError (instead of "u") if used in a list comprehension Works as expected inside a list comprehension: >>> var = "u2" >>> [var.strip()[0] for i in range(2)] ['u', 'u'] >>> ["ok" for i in range(2) if var.strip()[0] == "u"] ['ok', 'ok'] I

[issue46302] IndexError inside list comprehension + workaround

2022-01-07 Thread Pierre Fortin
New submission from Pierre Fortin : var = "u2" var.strip()[0] Works as expected, except that it returns IndexError (instead of "u") if used in a list comprehension -- at least, that's where I found it. Attached example script illustrates the issue. Call it with "mytest N" where N is 1