[Tutor] List iteration

ibraheem umaru-mohammed ium@micromuse.com
Fri, 11 May 2001 11:46:18 +0100


[Sharriff Aina wrote...]
<snip>
-| I would like to do some list iteration, is there a way to do something like
-| this:
-| 
-| 
-| # code start
-| list_a = ['a', 'b', 'c']
-| list_b = ['d', 'e', 'f']
-| 
-| for x, y in [list_a, list_b]:
-| ....print  x + y          # hypo, this FAILS!! since the iterators length
-| is shorter than the number of elements in list_a or list_b
-| 
-| Any ideas?
-| 
<\snip>

Since, no one has replied yet, here is my attempt.

<interactive session>

	ibraheem@ignoramus:$ python
	Python 2.1 (#1, Apr 20 2001, 17:50:32)
	[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
	Type "copyright", "credits" or "license" for more information.
	>>> list1=['a','b','c']
	>>> list2=['d','e','f']
	>>> do_it=lambda x,y: x + y
	>>> for i in map(do_it,list1,list2):
	...     print i
	...
	ad
	be
	cf
	>>>    

<\interactive session>

-| 
-| Cheers Sharriff
-| 

Hope that helps a little.

kindest regards,

	--ibs.

-- 
For good, return good.
For evil, return justice.