[Python-ideas] More user-friendly version for string.translate()

Chris Angelico rosuav at gmail.com
Fri Oct 28 10:35:07 EDT 2016


On Sat, Oct 29, 2016 at 1:28 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> If one has a translation dictionary d, use that in twice in the genexp.
>
>>>> d = {'a': '1', 'b': '3x', 'c': 'fum'}
>>>> ''.join(d[c] for c in s if c in d.keys())
> 'fum11fumfumfum1'

Trivial change:

>>> ''.join(d[c] for c in s if c in d)
'fum11fumfumfum1'

ChrisA


More information about the Python-ideas mailing list