Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with pass-by-reference in Python 2d list comprehensions

Take this dict of dicts:

case_forms = {'plural': {'nominative': 'dni', 'locative': 'dniach'}, 
              'singular': {'instrumental': 'dniem', 'vocative': 'dzie\xc5\x84'}}

I'd like to get a list of all the (a,b) key-pairs that are usable as case_forms[a][b].

No problem, right? Double list comprehension. Do stuff like that all the time in Haskell:

[(number, case_name) for case_name in case_dict.keys() for number, case_dict in case_forms.items()]

Except this doesn't produce the result you would expect:

[('plural', 'instrumental'),
 ('singular', 'instrumental'),
 ('plural', 'vocative'),
 ('singular', 'vocative')]

I'm wondering how to fix this problem. No amount of cleverly placed [:] seems to do the trick.

like image 568
Tom Busby Avatar asked Feb 05 '26 17:02

Tom Busby


1 Answers

How about this :

[ (number, case_name) for number, case_dict in case_forms.items() for case_name in case_dict.keys() ]

Edited to reference @juanpa.arrivillaga's comments about why my example was behaving strangely:

Python 2 list comprehensions have leaky scope, and it's not using the case_dict you think it is, it's using the case_dict from a previous comprehension (that leaked into the external scope)

Start a new interpreter session and you'll get the NameError

like image 68
t.m.adam Avatar answered Feb 07 '26 05:02

t.m.adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!