[Tutor] Iterating through two lists at the same time with manipulation..

John Fouhy john at fouhy.net
Fri Jun 29 03:23:31 CEST 2007


On 29/06/07, Iyer <maseriyer at yahoo.com> wrote:
> I have 2 lists:
>
> List 1 has lists in it, such as
>
> list1 = [[1,'A'],[2,'B'],[3,'C'],[4,'D']]
>
> There is another list2 such as
>
> list2 = [[1,'AA'],[3,'CC'], [4,'DD']]
>
> For eg,
>
> I wish to iterate over both the lists and produce the output
>
> a = [[1,'A'],[1,'AA']]
> b = [[2,'B']]
> c = [[3,'C'],[3,'CC']]
> d = [[4,'D'],[4,'DD']]

Your best choice is probably just to do it "by hand":

i1 = 0
i2 = 0
output = []
while True:
    # compare list1[i] and list2[i2]
    # produce appropriate output
    # advance i1 or i2

Alternatively, you could shove the data into a sqlite in-memory
database, then use SQL to pull it back out.

-- 
John.


More information about the Tutor mailing list