Start United States USA — software An Introduction to Python Sets An Introduction to Python Sets

An Introduction to Python Sets An Introduction to Python Sets

516
0
TEILEN

Learn how to create a set, how to use the set constructor, about the methods of sets, about the different set operations, and how to iterate over sets.
Python supports sets, which are a collection of unique elements and provide operations for computing set unions, intersections, and differences.
A set is a collection of unique elements. A common use is to eliminate duplicate elements from a list. In addition, it supports set operations like union intersection and difference.
This involves brace construction and set comprehension.
Notice that the set contains unique elements only even though we put duplicates into it.
A set need not contain elements of the same type. You can mix and match element types as you like.
Create a set from a list using the set () constructor.
How about creating a set of characters comprising a string? This shortcut will work.
Creating a set of unique random numbers:
The following sections explain the most commonly used methods of sets.
The boolean expressions elem in a and elem not in a allow checking for membership of a set.
You can obtain the size of a set (the number of elements) using the len () function.
Use the add () method to add an element to the set. If the element does not exist, it is added. No errors are raised if the element does exist, though.
You will need to use a loop to add multiple elements since the add () method accepts only a single argument.
You cannot add a list to a set since the list cannot be hashed.
However, a tuple can be added since it is not mutable and hence hashable.
Remove a single element from a set using remove () .
A KeyError is raised if the element is not in the set. (Running the same code as above a couple of times generates a random sequence without 10 in the set.)
Need to remove an element from a set without the pesky KeyError? Use discard () .
Remove all elements from a set? Use clear () .
Let’s now learn about set operations supported by a set .
A set is disjoint with another set if the two have no common elements. The method isdisjoint () returns True or False as appropriate.
Another example:
Check whether all elements of a set are contained in another set using the issubset () method. You can also use the boolean form setA <= setB.
Using the form setA < setB checks for setA being a proper subset of setB (that is setB containing all elements from setA and then some more) .
Need to check for a superset? Use issuperset () or setA >= setB or setA > setB for a proper superset.
Compute the union of two or more sets using the union () method. A new set containing all elements of all sets is returned.
How about identifying elements common to two or more sets? Use the intersection () method or the & operator.
Set difference returns a new set containing all elements in the argument set that are not in the other sets.
There are several ways of iterating over sets, most common ones are presented here.
And that’s it for now with sets. We learned how to create sets using the brace notation as well as the set constructors. Next up were the various commonly used operations with sets.

Continue reading...