>>> y = set([1, 7, 8, 9]) >>> x | y

You can create a set by using set on a sequence, like a list O. When a sequence is made into a set, duplicates are removed ©. After creating a set using the set function, you can use add Q and remove O to change the elements in the set. The in keyword is used to check for membership of an object in a set ©. You can also use | Q to get the union, or combination, of two sets, & to get their intersection O, and * © to find their symmetric difference—that is, elements that are in one set or the other but not both.

These examples aren't a complete listing of set operations but are enough to give you a good idea of how sets work. For more information, refer to the official Python documentation.

Was this article helpful?

0 0

Post a comment