I'm doing some set operations in Python, and I noticed something odd..

```>> set([1,2,3]) | set([2,3,4])
set([1, 2, 3, 4])
>> set().union(*[[1,2,3], [2,3,4]])
set([1, 2, 3, 4])```

That's good, expected behaviour - but with intersection:

```>> set([1,2,3]) & set([2,3,4])
set([2, 3])
>> set().intersection(*[[1,2,3], [2,3,4]])
set([])```

Am I losing my mind here? Why isn't set.intersection() operating as I'd expect it to?

How can I do the intersection of many sets as I did with union (assuming the [[1,2,3], [2,3,4]]had a whole bunch more lists)? What would the "pythonic" way be?

May 24, 2018 in Python 524 views

## 1 answer to this question.

When you do set() you are creating an empty set. When you do set().intersection(...) you are intersecting this empty set with other stuff. The intersection of an empty set with any other collection of sets is empty.

If you actually have a list of sets, you can get their intersection similar to how you did it.

```>>> x = [{1, 2, 3}, {2, 3, 4}, {3, 4, 5}]
>>> set.intersection(*x)
set([3])```

You can't do this directly with the way you're doing it, though, because you don't actually have any sets at all in your example with intersection(*...). You just have a list of lists. You should first convert the elements in your list to sets. So if you have

`x = [[1,2,3], [2,3,4]]`

you should do

`x = [set(a) for a in x]`
• 7,720 points

## i am normalizing the data set iris in python and get the error ::TypeError: 'numpy.float64' object is not callable

TRY THIS   #Nomalisation for i in names:     print(i)   ...READ MORE

## what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

+1 vote

## What do you mean by python scripting? What is a script and a module in python?

A module is a file containing a ...READ MORE

## Can someone explain the behaviour of increment and decrement operators in python

down voteaccepted ++ is not an operator. It is ...READ MORE

## Create and open a file in Python

file = open('text.txt', 'w+) READ MORE

## Using Lists and Tuples in Python

if you are familiar with C programming, ...READ MORE

## *args and **kwargs in python

In cases when we don’t know how ...READ MORE