954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Two List Difference

I have two similar lists and want to create a new list with just the differences between the two. Is there an existing function or do I have to iterate with a for loop?

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

You have to select one of the lists as reference, if they are not of the same length, use the longer list. Here is an example using the standard for loop and the simpler list comprehension ...

# check two lists for the difference

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = [1, 2, 3, 4, 4, 6, 7, 8, 11, 77]

def list_difference(list1, list2):
    """uses list1 as the reference, returns list of items not in list2"""
    diff_list = []
    for item in list1:
        if not item in list2:
            diff_list.append(item)
    return diff_list

print list_difference(list1, list2)  # [5, 9, 10]

# simpler using list comprehension
diff_list = [item for item in list1 if not item in list2]

print diff_list    # [5, 9, 10]
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Thank you for taking the time to explain this - it really helped me just now.

You have to select one of the lists as reference, if they are not of the same length, use the longer list. Here is an example using the standard for loop and the simpler list comprehension ... [php]# check two lists for the difference

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list2 = [1, 2, 3, 4, 4, 6, 7, 8, 11, 77]

def list_difference(list1, list2): """uses list1 as the reference, returns list of items not in list2""" diff_list = [] for item in list1: if not item in list2: diff_list.append(item) return diff_list

print list_difference(list1, list2) # [5, 9, 10]

# simpler using list comprehension diff_list = [item for item in list1 if not item in list2]

print diff_list # [5, 9, 10] [/php]

gmilby
Newbie Poster
8 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Hi,
List1=[5,8,9,11]
List2=[8,9]
List3=list(set(List1-List2))
print List3 #[5,11]

selvamarcadu
Newbie Poster
2 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
Hi, List1=[5,8,9,11] List2=[8,9] List3=list(set(List1-List2)) print List3 #[5,11]

this might work in some cases, but if it data isn't consistent and inline it fails:
Traceback (most recent call last):
File "list-diff44.py", line 3, in
lst3=list(set(lst1-lst2))
TypeError: unsupported operand type(s) for -: 'list' and 'list'

adjusted your lines by adding a few irregulars:
1 lst1=[1,2,3,45,5,8,9,11]
2 lst2=[8,9]
3 lst3=list(set(lst1-lst2))
4 print lst3

what i was comparing was domain table names against actual table names in a db.

i ended up having to use a for loop

...get db query/fetch_all() tables
...read in ini file (parsing out everything except a list of the table names)
then i actually found the code i needed here - if i can find it i'll paste a link to what worked.

either way it was something like this:
1 def Compare(list2,list1):
2 for i in list2:
3 for j in list1:
4 if i==j:
5 print list1[0]
6 #return true
7 if i>j:
8 break
9 print list[0]
10 return false

gmilby
Newbie Poster
8 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
Hi, List1=[5,8,9,11] List2=[8,9] List3=list(set(List1-List2)) print List3 #[5,11]

useList3=list(set(List1)-set(List2))
and it will work

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 
use List3=list(set(List1)-set(List2)) and it will work


yep that worked... simple too - i doubt python will ever deprecate the set tag *knocks on wood*

1 lst1=[1,2,3,45,5,8,9,11]
2 lst2=[8,9]
3 lst3=list(set(lst1)-set(lst2))
4 print lst3

gmilby
Newbie Poster
8 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Actually, Python3 has modernized the set() function.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 
Hi, List1=[5,8,9,11] List2=[8,9] List3=list(set(List1-List2)) print List3 #[5,11]
use List3=list(set(List1)-set(List2)) and it will work


Oops,that is what i tried to mean,but mentioned it wrongly.Thanks for pointing it out :)

selvamarcadu
Newbie Poster
2 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 
Actually, Python3 has modernized the set() function.

Any ideas when python 3 will be the norm? it doesn't appear ANYONE is racing forward... have to wonder how extensive the bugs are in the new version...

gmilby
Newbie Poster
8 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
Any ideas when python 3 will be the norm? it doesn't appear ANYONE is racing forward... have to wonder how extensive the bugs are in the new version...


On the contrary, everybody is racing forward. The problem is that we all use third party python modules which are not yet compatible with python 3. I think that a good alternative is to install both python 2.6 and python 3.last on your machine and try to write code which runs under both pythons. This way, you learn python 3 and at the same time, you write code usable by everybody.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You