472,328 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Standard ways to get union, intersection, difference of lists?

Hi!

Are there any standard list methods for getting the intersection and
difference of two lists? (The union is easy ("list1.extend(list2)"),
unless you want it to contain unique values.)

Here is what I would like to have:

list1 = [1,2,3]
list2 = [3,4]

list3 = list1.intersection(list2)
(list3 is now [3])

list3 = list1.difference(list2)
(list3 is now [1,2])

list3 = list2.difference(list1)
(list3 is now [4])

I realize I could quite easily implement this myself, but I was hoping for
a built-in solution.

Cheers,

/Mickel

--
Mickel Grönroos, application specialist, linguistics, Research support,CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi
Jul 18 '05 #1
3 80810
Mickel Grönroos wrote:
Are there any standard list methods for getting the intersection and
difference of two lists? (The union is easy ("list1.extend(list2)"),
unless you want it to contain unique values.)


If you have Python 2.3 available, why not use a set?
import sets
s1 = sets.Set([1, 2, 3])
s2 = sets.Set([3, 4])
s1.intersection(s2) Set([3]) s1.difference(s2) Set([1, 2]) s2.difference(s1)

Set([4])

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Only the ephemeral is of lasting value.
\__/ Ionesco
Jul 18 '05 #2
>>>>> "Mickel" == Mickel Grönroos <mi****@csc.fi> writes:
Hi! Are there any standard list methods for getting the
intersection and difference of two lists? (The union is easy
("list1.extend(list2)"), unless you want it to contain unique
values.)
[snip]
I realize I could quite easily implement this myself, but I was
hoping for a built-in solution.


You might want to take a look at the sets module in Python 2.3. It's
not exactly what you ask for since sets.Sets are not lists - but they
do support these operations and can easily be converted to or from
lists.
Jul 18 '05 #3
xoanan
1
I know lots of people hate using filter and lambda calls but this will give you a union between 2 lists, example:

[COLOR=Green]list1=range(5) # [0,1,2,3,4]
list2=range(3,7) # [3,4,5,6]
union=list1+filter(lambda x:x not in list1,list2)
# union = [0,1,2,3,4,5,6]
[/COLOR]

Intersection is just as easy

[COLOR=Green]
intersection=filter(lambda x:x in list1,list2)
# intersection=[3,4]
[/COLOR]

Difference is the last thing you wanted...
[COLOR=Green]
difference=filter(lambda x:x not in list2,list1)
# difference=[0,1,2]
[/COLOR]

And my set vocabulary is rusty but the distinct elements or those from both lists that are NOT in common (un-intersection?) would be:

[COLOR=Green]
distinct=filter(lamba x:x not in list2,list1)+filter(lambda x:x not in list1,list2)
# distinct=[0,1,2,5,6]
[/COLOR]

. -. -.. - .-. .- -. ... -- .. ... ... .. --- -.
Xoanan
Aug 9 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Todd MacCulloch | last post by:
Suppose I have a master list and I need to create two derived lists, something like: s0 = s1 = s2 = But suppose generating s0 is...
17
by: Gordon Williams | last post by:
Hi, I have to lists that I need to find the common numbers (2nd rounded to nearest integral) and I am wondering if there is a more efficient way...
7
by: Will | last post by:
I'm trying to build dynamic sql from a string passed by a calling application. Let's assume for this discussion that the user can pass a string of...
2
by: James Stroud | last post by:
Hello All, I find myself in this situation from time to time: I want to compare two lists of arbitrary objects and (1) find those unique to the...
12
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
3
by: GKRAFT | last post by:
I'm trying to create a function which would look like this when running: >> union(,) >> union(,) I have written the following code: def...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.