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

[3,4]

4 comments

@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

@user3917838

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 ]))

where:

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 = sets.next()
    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

set.intersection(*map(set,d))

@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)

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

@Legend, what version of python are you on? it works for me.

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

@AaronMcSmooth: 2.4.3?

@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 en.wikipedia.org/wiki/Empty_product it would be the only set which would be guaranteed to not change the result of an intersection.

Related Questions

Sponsored Content

45 Answered Questions

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

26 Answered Questions

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

7 Answered Questions

[SOLVED] How do I get the number of elements in a list?

  • 2009-11-11 00:30:54
  • y2k
  • 3151484 View
  • 1837 Score
  • 7 Answer
  • Tags:   python list

16 Answered Questions

[SOLVED] What are metaclasses in Python?

29 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3497310 View
  • 2870 Score
  • 29 Answer
  • Tags:   python list indexing

62 Answered Questions

[SOLVED] Calling an external command from Python

23 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

20 Answered Questions

28 Answered Questions

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

  • 2008-09-10 06:20:11
  • Ray Vega
  • 2512520 View
  • 3235 Score
  • 28 Answer
  • Tags:   python list

31 Answered Questions

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

Sponsored Content