Invalid syntax using dict comprehension
Dict comprehensions are only available in Python 2.7 upwards. For earlier versions, you need the dict() constructor with a generator: dict((x1, (x0,x2)) for (x0,x1,x2) in zip(x[:-2],x[1:-1],x[2:]))
Dict comprehensions are only available in Python 2.7 upwards. For earlier versions, you need the dict() constructor with a generator: dict((x1, (x0,x2)) for (x0,x1,x2) in zip(x[:-2],x[1:-1],x[2:]))
Yes! Python 3.8 introduces the “Assignment operator” :=, which allows you to define a variable within the local scope of a single expression (e.g. a comprehension). In your example, you would do this: result = {(p := next(k for k in (‘path’, ‘subdir’) if k in e)): some_func(p) for e in bad_structure} Disclaimer: this will … Read more
A dict comprehension has its own namespace, and locals() in that namespace has no a. Technically speaking, everything but the initial iterable for the outermost iterable (here [“a”]) is run almost as a nested function with the outermost iterable passed in as an argument. Your code works if you used globals() instead, or created a … Read more
{inner_k: myfunc(inner_v)} isn’t a dictionary comprehension. It’s just a dictionary. You’re probably looking for something like this instead: data = {outer_k: {inner_k: myfunc(inner_v) for inner_k, inner_v in outer_v.items()} for outer_k, outer_v in outer_dict.items()} For the sake of readability, don’t nest dictionary comprehensions and list comprehensions too much.
No, there is not. A dict comprehension produces a new item for each iteration, and your code needs to produce fewer items (consolidating values). There is no way to access keys produced in an earlier iteration, not without using (ugly, unpythonic) side-effect tricks. The dict object that is going to be produced by the comprehension … Read more
You’ve already got it: A if test else B is a valid Python expression. The only problem with your dict comprehension as shown is that the place for an expression in a dict comprehension must have two expressions, separated by a colon: { (some_key if condition else default_key):(something_if_true if condition else something_if_false) for key, value … Read more
Use: gw_func_dict = dict((chr(2**i), func) for i, func in enumerate(gwfuncs[:8])) That’s the dict() function with a generator expression producing (key, value) pairs. Or, to put it generically, a dict comprehension of the form: {key_expr: value_expr for targets in iterable <additional loops or if expressions>} can always be made compatible with Python < 2.7 by using: … Read more
You can use a generator expression: tuple(i for i in (1, 2, 3)) but parentheses were already taken for … generator expressions.
Use a dict comprehension (Python 2.7 and later): {key: value for (key, value) in iterable} Alternatively for simpler cases or earlier version of Python, use the dict constructor, e.g.: pairs = [(‘a’, 1), (‘b’, 2)] dict(pairs) #=> {‘a’: 1, ‘b’: 2} dict([(k, v+1) for k, v in pairs]) #=> {‘a’: 2, ‘b’: 3} Given separate … Read more