#puzzle for #python fans:
where does this error come from?
In my opinion, one of the great things about #python is that it's simple and straightforward - you don't get unexpected surprises. But there are a few exceptions, as this example shows.😉
Is a weird one. Don't think I encountered any place or problem to use it except for mathematical operations, where I would normally just lean towards #Numpy.
But I'll keep it in mind. I like functions like these, which force me to change my solutions to use optimized built-ins rather than my own solutions.
One thing I discovered is that zip is sometimes more useful for dictionaries than lists. The broad Itertools class always makes us think lists, sets, and tuples, but not dictionaries
Most of these are nice. But once you hit a certain size, these functions become slow, and using hash maps and indexing is faster. So I would use this for less than 100 keys.
Most devs would lose it if you have a dict with 100 keys, but yeah. At that size, hashing is the same speed as the lookup or just a simple class if you uave the memory for it.
Most of the time I try to do stupid stuff like this rather than solving the actual problem in my own private projects.
Dictionary comprehension only changes the outer operators. This changes the inner logic an differentiates it from other logic. Thus, for me, my brain sees mistakes easier by keeping it separate from list comprehension.
List and dictionary comprehension is fun, but the built-in functions are implemented in C. Thus, they are faster and easier to spot mistakes.
So I use KISS for comprehension, anything more advanced I lean on built-ins.
@djvdq@folkerschamel But surely zip is a different result - of the same length as the original sequences (ie 2)? Rather than the "square" (ie 4) from the nested comprehension loops.
@folkerschamel I wouldn’t call chained comprehensions simple, and mostly advise against them, especially for this kind of scoping problem you’ve highlighted.
@folkerschamel@ljmc I used to struggle with it too, but now I remember the ordering by mentally converting it too a multiline for-loop again, and keep the same order.
Add comment