[Baypiggies] Pythonic way to iterate over two lists?

Mark Voorhies mvoorhie at yahoo.com
Tue Jun 7 18:34:41 CEST 2011


On Monday, June 06, 2011 10:43:41 pm Simeon Franklin wrote:
> I taught a Python Fundamentals class last week for Marakana and
> noticed that other programmers coming from languages that are not
> specifically functionally oriented were unfamiliar with zip as a
> concept. Most explanations of zip tend to focus on the two case (given
> two lists it returns paired elements) and the more general Python
> documentation explanation was met with thoughtful incomprehension:
> 
> >This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
> 
> When I paraphrased this as "zip will take arguments that represent
> rows of input data and return a list whose elements are the columns of
> the input data" mental lightbulbs went on all over the room. YMMV but
> I thought it made for an intuitive explanation... It also leads me to
> think more naturally of possible applications of zip and iterative
> friends.

Yes!
transpose_A = zip(*A)
# if A is, e.g., a rectangular matrix as a list of lists

Thanks for the very useful point of view =)

--Mark

> 
> -regards
> Simeon Franklin
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
> 


More information about the Baypiggies mailing list