NEWBIE QUESTIION: Comparing Lists in Python

Ken Seehof kseehof at neuralintegrator.com
Wed May 1 23:17:59 EDT 2002


> Hi everyone,
>
> I haven't been using python for very long, so I hope you will
> forgive me if
> I am asking a stupid question, or doing this in a stupid way.
>
> I am trying to compare two lists in Python, which have been
> passed into it.
> I want to return the difference between the two lists as again two lists
>
> eg:
>
> list 1 = item1, item5, item8, item10
> list2 = item 3, item4, item5

I think what you mean is:

list1 = [item1, item5, item8, item10]
list2 = [item 3, item4, item5]

Lists have square brackets [] around them.

> I want to return
>
> returnlist1 = item1, item8, item10
> returnlist2 = item3, item4
>
> I haven't been working in python very long, but from what I have read, the
> best way is to convert these to a dictionary, and compare there.  This is
> what I have done so far.
>
> list1 = list1.split(',')
> list2 = list2.split(',')

Apparently your inputs are strings, not lists.  I don't know what your
input looks like exactly, but if it contains spaces after the commas,
the above code won't work right.  Where is the input coming from?  Can
you give us an example?

The advice you are referring to looks something like this:

dict1 = {}
for item in list1:
    dict1[item]=item

dict2 = {}
for item in list2:
    dict2[item]=item

Then you copy items from list1 to returnlist1 excluding those that are
in dict2.  And again for returnlist2.  I'll let you write that code for
practice :-)

The purpose of converting to a dictionary is to improve speed.  You
don't convert the dictionaries back to lists.  Instead, copy items one
at a time from list1 to returnlist, excluding what's in dict2.

> returnlist1 = []
> returnlist2 = []
>
> d = {}
> for item in list1:
>   if item not in list2:
>    d[item] = item
> if d:
>  returnlist1 = d.values()
>
> d2 = {}
> for item in list2:
>  if item not in list1:
>     return item         <--- huh?
>     d2[item] = item
> if d2:
>  returnlist2 = d2.values()
>
> return [returnlist1,returnlist2]

The "return item" will exit your function prematurely.  I'm sure that's
not what you want.

> I found if I didn't include the split, but just passed in a list like
> "list1=[item1, item3, item] it would work only on 1 character at
> a time and
> not recognise it was a split.
>
> Please let me know if I am doing this in a very stupid way.  The
> problem is
> that it isn't always recognising the lists, or comparing correctly.  It
> finds returnlist1 OK, but not returnlist2.
>
> I'm using python scripts in Zope if that may also be causing a problem.

Zope is the reason your inputs are strings rather than lists.

I'd recommend getting something to work in a local python interpreter first.
After you get it is working, add it to Zope.

In general, it is best to program one little piece at a time, that way
you will only have to solve one problem at a time.  Divide and conquer.

You should play around in the python interpreter until you are more
comfortable with python before doing Zope programming.

When you are ready to do Zope, try some really simple test program.
Try to get to know your raw materials.  Don't do anything complex.
For example, just write a one line program like this:

  return [list1, list2]

Then try this:

  return split(list1)

Play around with it until you can consistently guess the result every
time.  Try adding two numbers together in Zope.  That kind of thing.

> Thanks very much
>
> Di
> --
> Dianne van Dulken
> (put out the cats to talk to me)
>
> See the cutest puppy in the known universe (as voted by Rick and i)
> http://www.dogmac.com/bartholomew

Bart gets another vote from me. :-)

- Ken Seehof






More information about the Python-list mailing list