Condense list into string : ['z','y','x'...] -> 'zyx...' ? Python (2.7.1)

O.rka picture O.rka · Apr 20, 2011 · Viewed 8.1k times · Source

If I have list='abcdedcba'

and i want: a=z, b=y, c=x, d=w, e=v so it would translate to:

translate='zyxwvwxya'

How would I do this? If I construct a dictionary

>>> d=dict(zip(('a','b','c','d','e'),('z','y','x','w','v')))

and type

>>> example= d[x] for x in list
>>> print translate
['z', 'y', 'x', 'w', 'v', 'w', 'x', 'y', 'z']

How do I get it back into the form

translate='zyxwvwxyz'

?

Answer

eat_a_lemon picture eat_a_lemon · Apr 20, 2011
the_list = ['z', 'y', 'x', 'w', 'v', 'w', 'x', 'y', 'z']
print "".join(the_list)