[Tutor] Finding the differences between two lists

शंतनू shantanoo at gmail.com
Thu Aug 25 21:26:58 CEST 2011


You may try using set instead of list.

>>> verifiedList = {[1,2,3}
>>> scanResults = {1,2,3,4,5}
>>> badMacs = scanResults - verifiedList
>>> badMacs
set([4, 5])
>>> verifiedList = {1,2,3,7,8,9}
>>> badMacs = scanResults - verifiedList
>>> badMacs
set([4, 5])

-- 
shantanoo
On 26-Aug-2011, at 12:26 AM, Justin Wendl wrote:

> Hello,
> 
> Bare with me, as I am new to Python and a beginner programmer.
> I am trying to compare two lists (not of the same length), and create a new list of items that are -not- found in both lists.
> 
> Scenario: I have a list of MAC addresses that are known and good, and am comparing it to a list of MACs found in a scan.  I want to weed out the those which are unknown.  I am using IDLE (Python 2.7) on Windows, and all files are in the same directory.
> 
> Code:
> 
> scanResults = open('scanResults.txt', 'r')
> verifiedList = open('verifiedList.txt', 'r')
> badMacs = []
> 
> for mac in scanResults:
>   if mac not in verifiedList:
>     print mac
>     badMacs.append(mac)
>   else:
>     break
> 
> 
> When I print 'badMacs', the results come up empty:
> []
> 
> 
> Any help would be greatly appreciated!
> 
> Regards,
> Justin
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list