silly idea - interesting problem

Joshua Macy l0819m0v0smfm001 at
Sat Oct 20 14:15:32 CEST 2001

Stefan Antoni wrote:

> i got a silly idea about a small script which takes to strings (lets say
> "test" and "dust") and mixes them to "tdeusstt".
> i tried to code it because i thought it would be easy, but now i cannot
> find out _how_ to do it ;)
> i tried to "list()" the string and mix the items. but ...
> Anybody has a hint or even a solution?

   The simplest way I can think of to take items in pairs from two 
sequences is to use the builtin zip function.

   >>> a = "test"
   >>> b = "dust"
   >>> c = zip(a, b)
   >>> c
   [('t', 'd'), ('e', 'u'), ('s', 's'), ('t', 't')]

   Then it's a simple matter of iterating over the zipped sequence of 
tuples to format them into the single string that you want.


More information about the Python-list mailing list