1
for s in strategies:
        strats_having_fcs = {a.strategy: a.algorithmType for a in s.algorithms if a.algorithmType == AlgorithmTypeEnum.feedback_control.value}

can we make it in single line by comprehension?

Jwalin Shah
  • 2,451
  • 1
  • 17
  • 22

2 Answers2

2

Just:

strats_having_fcs = {a.strategy: a.algorithmType for s in strategies for a in s.algorithms if a.algorithmType == AlgorithmTypeEnum.feedback_control.value}

Have a look at this SO question about within-list comprehension. For me this clarified a lot how the logic behind this works.

NKahle
  • 142
  • 6
1

IIUC, perhaps you are looking for this nested comprehension

strats_having_fcs = {a.strategy: a.algorithmType for s in strategies for a in s.algorithms 
                     if a.algorithmType == AlgorithmTypeEnum.feedback_control.value}
Sheldore
  • 37,862
  • 7
  • 57
  • 71