{"id":25051,"date":"2021-11-10T09:37:29","date_gmt":"2021-11-10T04:07:29","guid":{"rendered":"https:\/\/python-programs.com\/?p=25051"},"modified":"2021-11-10T09:37:29","modified_gmt":"2021-11-10T04:07:29","slug":"python-set-intersection-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-intersection-method-with-examples\/","title":{"rendered":"Python Set intersection() Method with Examples"},"content":{"rendered":"
Python set() Function with Examples<\/span><\/a><\/p>\n Set intersection() Method in Python:<\/strong><\/p>\n The intersection() method returns a set containing the similarity(common items) of two or more sets.<\/p>\n If the comparison is done with more than two sets, the returned set contains only items that exist in both sets, or in all sets if the comparison is done with more than two sets.<\/p>\n For Example:<\/strong><\/p>\n If P and Q are two distinct sets. A set intersection between P and Q is :<\/p>\n Let P={4,5,6,7}<\/p>\n Q={5,6,8,9}<\/p>\n P intersection Q = {5,6}<\/p>\n Since {5,6} are only the common elements in both the sets<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n set1:<\/strong> This is Required. The set to look for similar items in<\/p>\n set2: <\/strong>This is Optional. The other set to look for similar items in. You are free to compare as many sets as you want. Use a comma to separate the sets.<\/p>\n Return Value:<\/strong><\/p>\n The intersection() method returns the intersection of set A and all the other sets (passed as argument).<\/p>\n If the argument is not passed to an intersection(), a shallow copy of the set is returned (A).<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Prerequisite: Python set() Function with Examples Set intersection() Method in Python: The intersection() method returns a set containing the similarity(common items) of two or more sets. If the comparison is done with more than two sets, the returned set contains only items that exist in both sets, or in all sets if the comparison is …<\/p>\nset.intersection(set1, set2,..........)<\/pre>\n
Given first set = {10, 20, 30, 40, 50}\r\nGiven second set = {100, 20, 80, 70, 30}\r\nGiven third set = {200, 40, 80, 30}<\/pre>\n
firstset intersection secondset = {20, 30}\r\nfirstset intersection thirdset = {40, 30}\r\nsecondset intersection thirdset = {80, 30}<\/pre>\n
Given first set = {'a', 'b', 'c'}\r\nGiven second set = {'p', 'q', 'r'}\r\nGiven third set = {'q', 'a', 'k'}\r\n<\/pre>\n
firstset intersection secondset = set()\r\nfirstset intersection thirdset = {'a'}\r\nsecondset intersection thirdset = {'q'}<\/pre>\n
Set intersection<\/strong>() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the first set as static input and store it in a variable.\r\nfst_set = {10, 20, 30, 40, 50}\r\n# Give the second set as static input and store it in another variable.\r\nscnd_set = {100, 20, 80, 70, 30}\r\n# Give the third set as static input and store it in another variable.\r\nthrd_set = {200, 40, 80, 30}\r\n# Get the intersection(common elements) of the first set and second set using\r\n# the intersection () method and print it.\r\nprint(\"firstset intersection secondset = \", fst_set.intersection(scnd_set))\r\n# Get the intersection(common elements) of the first set and third set using\r\n# the intersection () method and print it.\r\nprint(\"firstset intersection thirdset = \", fst_set.intersection(thrd_set))\r\n# Get the intersection(common elements) of the second set and third set using\r\n# the intersection () method and print it.\r\nprint(\"secondset intersection thirdset = \", scnd_set.intersection(thrd_set))\r\n<\/pre>\n
firstset intersection secondset = {20, 30}\r\nfirstset intersection thirdset = {40, 30}\r\nsecondset intersection thirdset = {80, 30}<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the first set as user input using set(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\nfst_set = set(map(int, input(\r\n 'Enter some random Set Elements separated by spaces = ').split()))\r\n# Give the second set as user input using set(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\nscnd_set = set(map(int, input(\r\n 'Enter some random Set Elements separated by spaces = ').split()))\r\n# Give the third set as user input using set(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\nthrd_set = set(map(int, input(\r\n 'Enter some random Set Elements separated by spaces = ').split()))\r\n \r\n# Get the intersection(common elements) of the first set and second set using\r\n# the intersection () method and print it.\r\nprint(\"firstset intersection secondset = \", fst_set.intersection(scnd_set))\r\n# Get the intersection(common elements) of the first set and third set using\r\n# the intersection () method and print it.\r\nprint(\"firstset intersection thirdset = \", fst_set.intersection(thrd_set))\r\n# Get the intersection(common elements) of the second set and third set using\r\n# the intersection () method and print it.\r\nprint(\"secondset intersection thirdset = \", scnd_set.intersection(thrd_set))\r\n<\/pre>\n
Enter some random Set Elements separated by spaces = 1 2 3\r\nEnter some random Set Elements separated by spaces = 1 5 7\r\nEnter some random Set Elements separated by spaces = 9 8 0\r\nfirstset intersection secondset = {1}\r\nfirstset intersection thirdset = set()\r\nsecondset intersection thirdset = set()<\/pre>\n