[Chicago] Nested List Comprehension in PyDev

JongMan Koo jongman at gmail.com
Tue Oct 16 04:14:17 CEST 2012


> In [136]: [(k, x) for x in d[k] for k in d]

This is actually equivalent to:

ret = []
for x in d[k]:
  for k in d:
     ret.append((k, x))

so the undefined exception for k is actually legit. I would guess it
was working in IPython because you already had k in your scope. You
should change it to:

> In [136]: [(k, x) for k in d for x in d[k]]

for the intended behavior.

Cheers,
JM


2012/10/15 Oren Livne <livne at uchicago.edu>:
> Dear All,
>
> If I type this in ipython, I get
> In [135]: d = {1: [2,3], 2: [4,5]}
>
> In [136]: [(k, x) for x in d[k] for k in d]
> Out[136]: [(1, 4), (2, 4), (1, 5), (2, 5)]
>
> The second command in Eclipse PyDev (using the same python installation)
> says "Undefined variable: k" for the k in the string "d[k]".
>
> What's wrong with PyDev?
> Thanks,
> Oren
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago


More information about the Chicago mailing list