0

How Can This Code Be Simplified? That is the question.

    stat_keys.append(self.request.get("stat_one"))
    stat_keys.append(self.request.get("stat_two"))
    stat_keys.append(self.request.get("stat_three"))
    stat_keys.append(self.request.get("stat_four"))
Rohit Rayudu
  • 3,765
  • 6
  • 21
  • 21

4 Answers4

3
stat_keys += [self.request.get(k) for k in ('stat_one', 'stat_two', 'stat_three', 'stat_four')]

or

stat_keys += map(self.request.get, ('stat_one', 'stat_two', 'stat_three', 'stat_four')))

or

map(stat_keys.append, map(self.request.get, ('stat_one', 'stat_two', 'stat_three', 'stat_four')))

I suggest the first one, though.

GaretJax
  • 7,462
  • 1
  • 38
  • 47
1

Difficult to answer without knowing what any of the variables are, but how about this:

for stat in ('stat_one', 'stat_two', 'stat_three', 'stat_four'):
    stat_keys.append(self.request.get(stat))

Or, using a generator expression:

[stat_keys.append(self.request.get(stat)) for stat in ('stat_one', 'stat_two', 'stat_three', 'stat_four')]
aquavitae
  • 17,414
  • 11
  • 63
  • 106
1

Change to .extend() with a generator expression:

stat_keys.extend(self.request.get(stat) for stat in('stat_one', 'stat_two', 'stat_three', 'stat_four'))
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
0
s_list = [ "stat_one", "stat_two", "stat_three", "stat_four" ]
[ stat_keys.append(self.request.get(s)) for s in s_list ]
Shawn Zhang
  • 1,680
  • 1
  • 12
  • 21