Difference between two lists in python - a simple solution

Sometimes, you have two lists in python and you need too know the difference between two lists. With a quick list comprehension, this is easy to find out:

numbers = [1,2,3,4,5,6,7,8]
even = [2,4,6,8,10]
difference = [ a for a in numbers if not a in even ]
print difference

The output should be:
[1, 3, 5, 7]

There you have it, a simple way to get the difference between two lists in python. :-)


Written by Jannich Brendle ons 11 juli 2012 In Programming

tags: list comprehensionpython

Related posts

social