### Thread: Union and Intersection of lists

1. #### Union and Intersection of lists

Hello,

Are there functions for obtaining the union and intersection of two lists or does it have to be coded?

2. I just found out that lists can be converted to sets and then using "and" their intersection can be obtained and using "or" their union, and finally "-" for obtaining elements found in one set but not the other.
My question now is whether a set is the same as a list in python without redundant elements?

As I have mentioned in my next posting also, it seems that Union and Intersection are not working properly, so I have added this editing.
Last edited by sam_kh918; February 27th, 2006 at 02:33 PM. Reason: Union and Intersection not working properly!
3. Originally Posted by sam_kh918
I just found out that lists can be converted to sets and then using "and" their intersection can be obtained and using "or" their union, and finally "-" for obtaining elements found in one set but not the other.
My question now is whether a set is the same as a list in python without redundant elements?
If by redundant you mean non-unique elements then yes.

grim
4. Could either of you post some code? I don't even know what a set is, but I'd be interested in seeing how you got that done!

Thanks!
5. Sure,
As far as I know, you can't get the union or intersection or the difference of the following list with functions
Code:
```>>> a=[3,5,6,8,12]
>>> b=[4,5,9,12]```
but if you convert them to sets as follows;
Code:
```>>> f=set(a)
>>> g=set(b)```
Now you can use "and", "or", and minus sign to get their intersection, union, and difference, repsectively;
Code:
```>>> f and g
set([9, 4, 5, 12])
>>> f or g
set([8, 3, 12, 5, 6])
>>> f-g
set([8, 3, 6])```

Ok it seems that the Union and Intersections are not correct. "f and g" returns set b and "f or g" returns set a, for some reason!
Last edited by sam_kh918; February 27th, 2006 at 02:29 PM. Reason: Intersection and Union are not correct!
6. Forgot to mention that, in case you don't know, the main difference b/w a list and a set is the lack of redundancy in sets.
For example if
a=[3,4,6,8,8,8,12,14,14]
then
set(a)=[3,4,6,8,12,14]
7. No Profile Picture
sfb
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
Nov 2003
Posts
624
Rep Power
34
Also, that sets are unordered - that is, when you iterate over them you may get the contents in any order, and you can't do:

Code:
```print myset[0]

or

myset[1:4]```
- because there is no order, the set doesn't know which elements 0, 1 or 4 are or what is between them.
8. Just wanted to emphasize that the Union and Intersection are not working as I thought, I have edited my previous posting.
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2003
Posts
53
Rep Power
11
Code:
```>>> import sets
>>> set1 = sets.Set([1,2,3,4])
>>> set2 = sets.Set([3,4,5,6])
>>> set1 & set2
Set([3, 4])
>>> set1 | set2
Set([1, 2, 3, 4, 5, 6])
>>> set1 - set2
Set([1, 2])
>>> set2 - set1
Set([5, 6])
>>> set1 ^ set2
Set([1, 2, 5, 6])```
there you have intersection, union, set difference (both ways) and symmetric difference.

oh, and just so you know. what you were doing before using "and" and "or" is a logical "and" (&&) and a logical "or" (||). it returns the last object it needed to evaluate in order to determine the value of the logical statement. in the case of the "and" it returns the second one because both need to be true for a true. for the "or" it returns the first one because it is true and automatically makes the statement true.