By Legend

2010-10-04 04:12:19 8 Comments

I am playing with python and am able to get the intersection of two lists:

result = set(a).intersection(b)

Now if d is a list containing a and b and a third element c, is there an built-in function for finding the intersection of all the three lists inside d? So for instance,

d = [[1,2,3,4], [2,3,4], [3,4,5,6,7]]

then the result should be



@A. John Callegari 2020-03-11 00:31:56

You can get the intersection of an arbitrary number sets using set.intersection(set1, set2, set3...). So you just need to convert your lists into sets and then pass them to this method as follows:

d = [[1,2,3,4], [2,3,4], [3,4,5,6,7]]  
set.intersection(*[set(x) for x in d])  


{3, 4}

@user3917838 2015-08-17 16:33:46

Lambda reduce.

from functools import reduce #you won't need this in Python 2
l=[[1, 2, 3, 4], [2, 3, 4], [3, 4, 5, 6, 7]]
reduce(set.intersection, [set(l_) for l_ in l])

@tj89 2017-09-20 06:08:39

you require a list of set , this would fail saying that 'descriptor intersection requires set'

@Bartek 2016-10-12 09:12:13


Nice and simple but needs some casting to make it work and give a list as a result. It should look like:

list(reduce(set.intersection, [set(item) for item in d ]))


d = [[1,2,3,4], [2,3,4], [3,4,5,6,7]]

And result is:

[3, 4]

At least in Python 3.4

@aaronasterling 2010-10-04 04:16:22

for 2.4, you can just define an intersection function.

def intersect(*d):
    sets = iter(map(set, d))
    result =
    for s in sets:
        result = result.intersection(s)
    return result

for newer versions of python:

the intersection method takes an arbitrary amount of arguments

result = set(d[0]).intersection(*d[:1])

alternatively, you can intersect the first set with itself to avoid slicing the list and making a copy:

result = set(d[0]).intersection(*d)

I'm not really sure which would be more efficient and have a feeling that it would depend on the size of the d[0] and the size of the list unless python has an inbuilt check for it like

if s1 is s2:
    return s1

in the intersection method.

>>> d = [[1,2,3,4], [2,3,4], [3,4,5,6,7]]
>>> set(d[0]).intersection(*d)
set([3, 4])
>>> set(d[0]).intersection(*d[1:])
set([3, 4])

@Legend 2010-10-04 04:19:26

@AaronMcSmooth: It gives me AttributeError: 'list' object has no attribute 'intersection' if I do that. Am I missing something?

@aaronasterling 2010-10-04 04:20:20

@Legend. you have to map it to a set first. I somehow missed the fact that they were lists. After that, you can just pass lists (or any other iterable) to the intersection method

@Legend 2010-10-04 04:24:22

@AaronMcSmooth: Actually, not sure why but I'm getting this error no matter what solution I try: TypeError: intersection() takes exactly one argument (3 given)

@aaronasterling 2010-10-04 04:26:24

@Legend. both my answer and TokenMacGuy's work for me on python 2.6 and 3.1

@Legend 2010-10-04 04:27:32

@AaronMcSmooth: Aah... Is there some solution for Python 2.4 by any chance?

@Legend 2010-10-04 04:38:15

@AaronMcSmooth: Today's a bad day. Sorry. When I define the intersection, it gives me: UnboundLocalError: local variable 'set' referenced before assignment I don't understand why it is considering set as a local variable.

@Legend 2010-10-04 04:46:45

@AaronMcSmooth: I edited your answer to something that works. Thanks for the help.

@aaronasterling 2010-10-04 04:51:52

@Legend. Thanks for cleaning up my post. but remember: 'Thus spoke the Lord: "Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are the devil's apples!"'

@Legend 2010-10-04 05:12:08

@AaronMcSmooth: Aye aye captain! Thou shall remember that for next time... Thanks! :)

@intuited 2010-10-04 08:41:09

Lobbeth thou thy holy space bar of Antioch.

@SingleNegationElimination 2010-10-04 04:18:50


@Legend 2010-10-04 04:20:45

Not sure what's wrong here. Now it gives me: TypeError: intersection() takes exactly one argument (2 given)

@Legend 2010-10-04 04:47:18

Thanks a lot. +1 for that. I just couldn't use it because of my Python version :(

@Aaron Robson 2012-09-26 23:51:23

If the d variable may have a length of zero the set.intersection function will raise a TypeError exception. I would recommend catching that exception and return set() (an empty set) instead in that degenerate case. This is better than checking the len of d beforehand as it may be a generator.

@SingleNegationElimination 2012-09-27 05:36:48

@AaronR: Good point, but the degenerate solution for set intersection is the universal set, not the empty set. since there's no compact way to represent that in python, raising an exception (probably catching the type error and raising something more sensible) is still probably the right way to handle it. Returning an empty set is the right thing to do for set union, however.

@Aaron Robson 2012-09-28 19:59:38

@TokenMacGuy: Yes, you're quite right; by analogy with it would be the only set which would be guaranteed to not change the result of an intersection.

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray
  • 2844611 View
  • 3234 Score
  • 27 Answer
  • Tags:   python list

27 Answered Questions

[SOLVED] How do I concatenate two lists in Python?

26 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

20 Answered Questions

18 Answered Questions

[SOLVED] How to clone or copy a list?

40 Answered Questions

[SOLVED] How to make a flat list out of list of lists?

62 Answered Questions

[SOLVED] Calling an external command from Python

30 Answered Questions

[SOLVED] Finding the index of an item in a list

  • 2008-10-07 01:39:38
  • Eugene M
  • 3865209 View
  • 3161 Score
  • 30 Answer
  • Tags:   python list indexing

20 Answered Questions

[SOLVED] What are metaclasses in Python?

32 Answered Questions

[SOLVED] "Least Astonishment" and the Mutable Default Argument

Sponsored Content