[Tutor] Using 'join ' function to create a string

Eric Brunson brunson at brunson.com
Fri Dec 21 17:33:31 CET 2007


lechtlr wrote:
> Hi there,
>
> I would like to know what is the best way to create a string object 
> from two different lists using 'join' function. For example, I have X 
> = ['a', 'b', 'c', 'd', 'e'] and Y = [1, 2, 3, 4, 5]. From X and Y, I 
> want to create a string Z = 'a:1, b:2, c:3, d:4, e:5'.

How about something like this:

", ".join( '%s:%s' % ( x, y ) for x, y in zip( X, Y ) )

Yields:  'a:1, b:2, c:3, d:4, e:5'


> Any help would greatly be appreciated.
> -Lex
>
> ------------------------------------------------------------------------
> Looking for last minute shopping deals? Find them fast with Yahoo! 
> Search. 
> <http://us.rd.yahoo.com/evt=51734/*http://tools.search.yahoo.com/newsearch/category.php?category=shopping> 
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   



More information about the Tutor mailing list