0

with the following snippet I can achieve what I'm looking for:

d = {}
d[1] = 'one'
d[2] = 'two'
d[3] = 'three'

exp = ''
for k, v in d.items():
    exp += '{}@1 + '.format(v)

exp = exp[:-3]

exp
'one@1 + two@1 + three@1'

I was wondering if there are some better solution than deleting the last characters.

matteo
  • 4,683
  • 9
  • 41
  • 77

1 Answers1

1

Using join:

d = {}
d[1] = 'one'
d[2] = 'two'
d[3] = 'three'

exp = ' + '.join('{}@1'.format(v) for v in d.values())    
print(exp)

OUTPUT:

one@1 + two@1 + three@1
DirtyBit
  • 16,613
  • 4
  • 34
  • 55