Sets are unordered collections of unique elements. Use curly braces {}
or the set()
function. They don't allow duplicate value
Sets offer the ability to manipulate groups of distinct/unique element-wise occurrences using standard set terminology and functionality related thereby rather than requiring use of indices etc. which only ever occur when sequence information explicitly available such as in arrays or lists which by design unlike Set itself retain sequence data too and may end up differing despite the exact elements used due to such positional metadata (unlike a set defined by distinct individual element-wise items rather than sequential arrangement thereof such as when defining in some order, or checking existence where an item must be found as in a list which despite any sorting would then preserve positions of those unique occurrences assuming unique data types, if so, while in a Set all order disregarded when looking since it could or would exist more or less by default within ordered context like List etc. without explicit enforcement You compare like items if suitable for purpose when deciding or similar such application of Boolean tests which differ notably from numeric sort used implicitly since otherwise there are potentially ambiguous behaviour if only testing where a sort assumes ordering at once unless type distinctions available at time. Set ops provide alternative when unique rather than ordering itself relevant as when merging datasets with and without shared items or columns given shared name when using some mapping method if available when testing since no ordering required only knowledge or verification of item occurrences rather than indices to specify what item is handled rather than relying exclusively on element name if it repeats within data and type changes apply for that data after updates/sorting to preserve some invariant such as positional sorting)
Method | Description | Example |
---|---|---|
union() |
Returns new set containing elements or members from both input sets even if similar or exact in value type, assuming type consistency applicable/appropriate |
set1 = {1, 2, 3}
set2 = {3, 4, 5} set3 = set1.union(set2) #Union print(set3) #Output: {1, 2, 3, 4, 5} |
intersection() |
returns elements shared in both sets if found, otherwise, intersection result empty because no matches, i.e., completely disjoint so you could perform checks using that to filter which further checks need occur if a step produces empty value assuming no intersection case irrelevant rather than causing crashes at a point like invalid indexing operations assuming such indices exist as could otherwise check to verify when required to specify how a non-intersecting sequence-style variable treated without that indice to test |
set1 = {1,2,3}
set2 = {3,4,5} set3 = set1.intersection(set2) print(set3) # Output:{3} because sets here using identical values of some int for simplicity/illustrative purposes, as opposed to testing on some key to ensure shared occurrences despite distinct objects such as rows in same table with same primary or index field where those fields not involved, only other values being validated for intersecting element conditions, such as filtering table one for some field based on other matching field column criteria being applied over contents or intersection values generated with set/dict when filtering records assuming they exist based upon criteria |
difference() |
Returns new set consisting elements found in set1 but not in set2. Set members used directly in value tests |
set1 = {1, 2, 3}
set2 = {3, 4, 5} set3 = set1.difference(set2) print(set3) # Output: {1, 2} |
symmetric_difference() |
new set, items exclusive to one or the other. Items shared between those being checked omitted in result since assumed these values are in some sense common already as in union method although this symmetric set differs notably if empty where union won't reduce this far if types or variable itself still in scope regardless matching content whereas symmetric comparison only looks for the unique-valued elements within individual Sets themselves at time. Such testing could in effect apply as filter like checking two table headers, by generating such result in-place, thereby identifying unshared row indices and filtering these from later intersection given field or columns if that makes sense here unlike simpler examples operating on similar or identical field type data in those prior Sets |
set1 = {1, 2, 3}
set2 = {3, 4, 5} set3 = set1.symmetric_difference(set2) print(set3) # Output: {1, 2, 4, 5} |