Thanks!It worked....i wasted a lot of time trying to find a bug in my code...what is wrong in iterating over a list and modifying it? Doesnt python take the modified list every time the loop starts? Also in this case, i want to add a condition that if none of the pairs are in pairList, element = []. How can i do that? > Le Lundi 12 Juin 2006 10:25, Girish Sahani a écrit : >> Hi, >> I am trying to modify a list of pairs (l4) by removing those >> pairs which are not present in a third list called pairList. >> The following is a simplified part of the routine i have written. >> However >> it does not give the correct output. Please help! >> Its possible i have made a trivial mistke since i am a newbie. >> >> def getl5(): >> l5 = [] >> pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]] >> l4 > >> [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]] >> for >> pair in l4: >> if pair not in pairList: >> l4.remove(pair) >> print "l4 is",l4 >> > > You are trying to modify a list while iterating over it, never do that ! > >> The output given is: >> l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]] > > Is this work in your case ? > > def getl5(): > pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]] > l4 = > [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]] > l4 = [ e for e in l4 if e in pairList ] > print "l5 is", l4 > > > -- > _____________ > > Maric Michaud > _____________ > > Aristote - www.aristote.info > 3 place des tapis > 69004 Lyon > Tel: +33 426 880 097 > -- > http://mail.python.org/mailman/listinfo/python-list >
-- http://mail.python.org/mailman/listinfo/python-list