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?

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]``````

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 ...

``````# 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]``````

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

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 <module>
lst3=list(set(lst1-lst2))
TypeError: unsupported operand type(s) for -: 'list' and 'list'

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

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

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

Actually, Python3 has modernized the set() function.

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 :)

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...

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.