{"id":25046,"date":"2021-11-10T09:37:18","date_gmt":"2021-11-10T04:07:18","guid":{"rendered":"https:\/\/python-programs.com\/?p=25046"},"modified":"2021-11-10T09:37:18","modified_gmt":"2021-11-10T04:07:18","slug":"python-set-difference-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-difference-method-with-examples\/","title":{"rendered":"Python Set difference() Method with Examples"},"content":{"rendered":"
Prerequisite:<\/h5>\n

Python set() Function with Examples<\/span><\/a><\/p>\n

Set difference() Method in Python:<\/strong><\/p>\n

The difference() method returns a set containing the difference of two sets.<\/p>\n

The returned set contains items that only exist in the first set and not in both.<\/p>\n

For Example:<\/strong><\/p>\n

If P and Q are two distinct sets. A set difference between P and Q is a set of elements that exist only in the set P but not in the set Q.<\/p>\n

Let P={4,5,6,7}<\/p>\n

Q={5,6,8,9}<\/p>\n

P-Q={4,7}<\/p>\n

Q-P ={8,9}<\/p>\n

Syntax:<\/strong><\/p>\n

set.difference(set)<\/pre>\n

Parameters<\/strong><\/p>\n

set:<\/strong> This is Required. The set used to look for differences.<\/p>\n

Return Value:<\/strong><\/p>\n

difference() computes the difference between two sets, each of which is a set. It makes no changes to the original sets.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given first set = {10, 11, 12, 13}\r\nGiven second set = {11, 12, 20, 14}<\/pre>\n

Output:<\/strong><\/p>\n

firstset-secondset :  {10, 13}\r\nsecondset-firstset :  {20, 14}<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given first set = {20, 30, 40, 20, 30, 50, 60}\r\nGiven second set = {40, 50, 60, 100, 80}<\/pre>\n

Output:<\/strong><\/p>\n

firstset-secondset :  {20, 30}\r\nsecondset-firstset :  {80, 100}<\/pre>\n

Set difference() Method with Examples in Python<\/h2>\n