3

I am creating a dictionaries that need condition inside. So here, I have an arr_temp holding some values, and I want to set paired_vals according to element in arr_temp (now works only for one case). I tried if statement, but facing an error that I can't use elif inside a comprehension. For now I'm just adding paired_vals_mass everywhere, but it's wrong for my situation, because I have several values that complement each element in arr_temp. Can anyone suggest a good way to implement that?

report['conditions'][condname] = 
{findings: [{'name': x, 'parameters': paired_vals_mass} for x in arr_temp]}
Ma0
  • 15,057
  • 4
  • 35
  • 65
Alice Jarmusch
  • 467
  • 1
  • 7
  • 20
  • you can nest if/else statements but will mess your code up – Netwave Aug 17 '17 at 09:48
  • Can you please show us the code you tried that failed? It is not clear what this condition is, and how you want to employ it in your code -- as such, a concise answer is not possible. – cs95 Aug 17 '17 at 09:49
  • you can implement an `if-else` conditional but no `elif` inside a dict-comprehension (or any other comprehension). If you need that, it would be better to drop the dict-comprehension – Ma0 Aug 17 '17 at 09:50
  • @Ev.Kounis so the only way to do it is just avoid dict-comprehension? – Alice Jarmusch Aug 17 '17 at 09:50
  • Not the only way, but the cleaner and better way. There is nothing wrong with _good-ol' looping_ – Ma0 Aug 17 '17 at 09:51
  • Python lambdas? https://stackoverflow.com/questions/33439434/multiple-if-statements-in-a-lambda-function – BoboDarph Aug 17 '17 at 09:52
  • It sounds like you need a function that returns the inner dict, given `x` and also uses an `if-else-elif` . – quamrana Aug 17 '17 at 09:52
  • @AliceJarmusch Like mentioned, it is possible. In a comprehension, `if ... elif` = `if else (if .... else ....)` – cs95 Aug 17 '17 at 09:55
  • 1
    Please show us some example input and output. – skrx Aug 17 '17 at 10:12

2 Answers2

6

This is how one would implement an elif in a comprehension:

a = {x: 'negative' if x < 0 else 'positive' if x > 0 else 'zero' for x in range(-2, 2)}
print(a)  # {0: 'zero', 1: 'positive', -1: 'negative', -2: 'negative'}

As you can see, there is no elif but a nested if. Even in this simple case, the code becomes less readable.

A better option would be a selector function like so:

def selector(x):
    if x < 0:
        return 'negative'
    elif x == 0:
        return 'zero'
    else:
        return 'positive'

a = {x: selector(x) for x in range(-2, 2)}
print(a)  # {0: 'zero', 1: 'positive', -1: 'negative', -2: 'negative'}
Ma0
  • 15,057
  • 4
  • 35
  • 65
2

It sounds like you need a function which constructs a dictionary:

def construct_dict(x, paired_vals_mass):
    ret = {'name': x}
    if x .... :
        ret['parameters'] = ...
    elif x .... :
        ret['parameters'] = ...
    else:
        ret['parameters'] = ...

    return ret

report['conditions'][condname] = 
    {findings: [construct_dict(x, paired_vals_mass) for x in arr_temp]}
quamrana
  • 37,849
  • 12
  • 53
  • 71