Python intersection output

python set intersection
python intersection of multiple lists
python intersection of lists
python set union
python set difference
python intersection of multiple sets
python set remove
python set pop
def union(A,B):
    k = A
    for i in B:
        k.add(i)
    return k
def intersection(A,B):
    k = set()
    for i in A:
        if i in B:
            k.add(i)
            print(k)
    return k
A = {1,2,3}
B = {1,2,4}
assert( union(A,B)== {1,2,3,4} )
assert( intersection(A,B) == {1,2} )

So I am just trying to do a simple intersection function. I think I can make it work another way if I really need to but I'm worried there's something major I missing so I figured I should ask here to better understand what is going on.

It is clear to me that the intersection of the two sets should be {1,2}, however for some reason, the for function iterating 'in' A pulls out a 4 when this is clearly not in the set A!

edit: posting my whole code here because apparently posting only the segment does not give an error.

Change the line of code k=A to k = set(A) to create a local copy of A and to not update the original set A as sets are mutable objects in python

A good read https://medium.com/@meghamohan/mutable-and-immutable-side-of-python-c2145cf72747

Python, Python program to illustrate the intersection. # of two lists in Output: [9, 11, 26, 28]. Method 2: This method includes the use of set() method. filter_none. edit Python Set intersection () method returns a new set with elements that are common to all the sets. Python Set intersection () method Syntax X.intersection (Y) is equivalent to X ∩ Y. X ∩ Y = Y ∩ X = The Set with the elements that are common to Set X and Y.

The problem comes from the union function. When you write k = A, k and A refer to the same set. So when you add elements to k, they are added to A. Then A is not the same when calling intersection(A,B)

Instead of k = A, write k = A.copy() to create a separate object.

Python intersection output, Change the line of code k=A to k = set(A) to create a local copy of A and to not update the original set A as sets are mutable objects in python. code. # Python program to illustrate the intersection. # of two lists in most simple way. def intersection (lst1, lst2): lst3 = [value for value in lst1 if value in lst2] return lst3. # Driver Code. lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69] lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26]

The intersection of A and B is easily achieved using built-in set functions.

>>> A = [1,2,3,'foo','bar']
>>> B = [2,4,'stack','overflow','foo']
>>> set(A) & set(B)
set([2, 'foo'])

Python Set intersection(), The intersection() method returns a new set with elements that are common to all Python Set intersection() When you run the program, the output will be: The intersection() function returns a set, which has the intersection of all sets(set1, set2, set3…) with set1. It returns a copy of set1 only if no parameter is passed. Below is the Python3 implementation of the above approach:

8.7. sets — Unordered collections of unique elements, Note, the non-operator versions of union() , intersection() , difference() , and Since sets only define partial ordering (subset relationships), the output of the  Output. The following output will appear after running the script. Three values are common to both lists. These values are 8, 9, and 20, and are shown in the content of the third list. Example 2: Intersecting Lists via the intersection() Method. There is no direct, built-in method for the list object to find out the intersection values between two lists.

Python set intersection, Output: {'a', 'e', 'b', 'd', 'c'} {'a', 'e'} {'a', 'e'}. Python Two Sets Intersection. The intersection() function takes multiple set arguments too. Let's look  Python: Tips of the Day. Python: How do I merge two dictionaries in a single expression in Python? For dictionaries x and y, z becomes a shallowly merged dictionary with values from y replacing those from x. In Python 3.5 or greater: z = {**x, **y} In Python 2, (or 3.4 or lower) write a function:

Intersection, union and difference of Sets in Python., The above program serves as a demonstration of how elements in a set are ordered, below is the output of the program where you can see the  This article demonstrates different operations on Python sets. Examples: Input : A = {0, 2, 4, 6, 8} B = {1, 2, 3, 4, 5} Output : Union : [0, 1, 2, 3, 4, 5, 6, 8

Comments
  • It returned {1,2} for me using your exact code.
  • I can't reproduce your problem. intersection({1,2,3}, {1,2,4}) returns {1,2}.
  • When asking a question, please explain what exactly is the problem. See also How do I ask a good question?
  • Not an answer (and your question is unanswerable as it stands), but I hope you're aware sets have an intersection method built in.
  • I don't understand why it returned the exact right thing for you guys...
  • Thanks! That worked... so setting an equals on a set... updates the set??? I don't understand why...
  • k = A just makes k another reference to the same set the name A refers to. You should read Facts and myths about Python names and values
  • This is very odd... since when is setting equals an excuse to update the right-hand side? Is it not like a rule that in most languages setting an equality only updates the left-hand side?
  • For objects like list, dict, set or custom objects, when you use the = sign, both sides refer to the same object, to avoid useless processing (copying a big object takes a lot of memory and time). Indeed most of the time this is enough. So when you want to create a copy, you have to use the copy method or use what mad_ described. But you must remember this is a costly operation.
  • Thanks for the answer but I am pretty sure OP wants to write his own intersection method