snippets / compare 2 lists

Language: Python Interactive Console - First posted by benoitc on 2008-01-25 16:00 (10 months)
Link to the snippet: http://www.friendsnippets.org/snippet/169/

easy way to compare lists, use sets :)

 1 >>> l1 = [2,3]
2 >>> l2 = [4,7,2,3,8,9]
3 >>> S1, S2 = set(l1), set(l2)
4 >>> S1
5 set([2, 3])
6 >>> S2
7 set([2, 3, 4, 7, 8, 9])
8 >>> S1 & S2
9 set([2, 3])
10 >>> S1 | S2
11 set([2, 3, 4, 7, 8, 9])
12 >>> S1-S2
13 set([])
14 >>> S2-S1
15 set([8, 9, 4, 7])
In order to post a comment, you should have a friendsnippet account. Please sign-in.

0 comments

Jan '08
  • easy way to compare lists, use sets :)

Common Tags


Related snippets

0 snippets in relation for now.


snippet History

Jan '08