Make Javascript do List Comprehension

generic case using Array.map, requires javascript 1.6 (that means, works on every browser but IE < 9) or with an object augmenting framework like MooTools works on every browser: var list_of_names = document.getElementsByTagName(‘input’).map( function(element) { return element.getAttribute(‘name’); } ); If dealing with an iterable that doesn’t provide map (such as generators), spread syntax can be … Read more

Line continuation for list comprehensions or generator expressions in python

[x for x in (1,2,3) ] works fine, so you can pretty much do as you please. I’d personally prefer [something_that_is_pretty_long for something_that_is_pretty_long in somethings_that_are_pretty_long] The reason why \ isn’t appreciated very much is that it appears at the end of a line, where it either doesn’t stand out or needs extra padding, which has … Read more

eval fails in list comprehension [duplicate]

By using list comprehension, you actually define a new scope. Indeed if we alter the list comprehension to: out2 = [print(globals()) or print(locals()) or eval(cmd) for cmd in [‘self.b’]] we force Python to print the local and global variables before making the eval(..) call, and we obtain something like: {‘__builtins__’: <module ‘builtins’ (built-in)>, ‘__name__’: ‘__main__’, … Read more