A dictionary would work well here. Read each element of the lists into
the dictionary using the integer as the key with a list of strings as
the values.

{1: ['A', 'AA'], 2: ['B'], 3: ['C', 'CC'], 4: ['D', 'DD']}

Then output the contents in the required format.

This may take more lines of code than other solutions, but it's simple
and flexible. If you can guarantee that this is not a class assignment,
I will pass on the code. It starts like this:

list1 = [[1,'A'],[2,'B'],[3,'C'],[4,'D']]
list2 = [[1,'AA'],[3,'CC'], [4,'DD']]
dictionary = {}
for i, j in list1+list2:


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of John Fouhy
Sent: Thursday, June 28, 2007 7:24 PM
To: Iyer
Cc: tutor@python.org
Subject: Re: [Tutor] Iterating through two lists at the same time
withmanipulation..

On 29/06/07, Iyer <[EMAIL PROTECTED]> 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.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to