Python

Constant dripping wears away a stone

Sets


Setsin Python are also used to store a collection of data. A set may contain items of same or different data types. The items in a set are enclosed with curly brackets and are separated with commas.

  • Set Properties:
    • Unordered and Unindexed: Sets are unordered. The items in a set do not have a defined order, and they can appear in a different order every time you use them. Thus a set item cannot be accessed by referring to an index number.
    • Unchangable: Once a set is created, the items cannot be changed. You can add new items or remove items from a set.
    • No Duplicates: Set items can be duplicates. Sets cannot have two items with the same value.
  • Access Set Items:
    • For in loop: A set item cannot be accessed by referring to an index number. You can use a forloop to loop through the set items.
  • Set Methods:
    • add(elem): Adds the elementelemto the given set, if the element does not already exist in the set; otherwise, the set stays unchanged.
    • clear(): Removes all items from the set.
    • copy(): Returns a new set which is a copy of the set.
    • difference(set): Returns a new set which contains the difference between the two sets.
    • difference_update(set): Removes the difference between the two sets from the original set that calls this method.
    • discard(value): Removes the item with the specified value from the set. If none such item exists, the set stays the same.
    • intersection(set1[, set2,...]): Returns a new set that contains all the items that exist in the original set and the argument sets.
    • intersection_update(set1[, set2,...]): Returns the original set that calls this method with only the items that exist in all the sets.
    • isdisjoint(set): Returns True if none of the items exist in both sets. Otherwise returns False.
    • issubset(set): Returns True if all items of the original set exist in the argument set. Otherwise returns False.
    • issuperset(set): Returns True if all items of the argument set exist in the original set. Otherwise returns False.
    • pop(): The original set is updated with a random item from the set being removed.
    • remove(value): Removes the item with the specified value from the set. If none such item exists, it will report an exception.
    • symmetric_difference(set): Returns a new set that contains all the items from both sets if the item does not exist in both sets.
    • symmetric_difference_update(set): Update the original set such that it contains all the items from both sets if the item does not exist in both sets.
    • union(iterable1[, iterable2...]): Returns a new set that contains all items from the original set and the argument(s) iterable(s). If an item exists in more than one iterables, it will be included only once in the result.
    • update(iterable1[, iterable2,...]): The original set is updated by adding all the items from the iterable(s). If an item exists in more than one iterables, the item will appear in the updated set only once.
  • Built-in Functions with Set:
    • all(a_set): Returns True if all elements in the seta_setare true or the set is empty; otherwise, returns False.
      • a_set: Required. A set.
    • any(a_set): Returns True if any element in the seta_setis true. If the set is empty or no element in the set is true, returns False.
    • enumerate(iter[, start]): Adds a counter that starts withstartto the iterableiter.and returns it as an enumerating object. Ifstartis not given, use 0 to start the counter.
      • iter: Required. An iterable.
      • start: Optional. A number from which the counter starts. By default, it is 0.
    • len(a_set): Returns the number of items in the set.
    • max(a_set): Return the maximum element in the seta_set.
    • min(a_set): Return the minimum element in the seta_set.
    • sum(iter[, start=0]): Adds all items of the iterableiterandstart and returns the sum. Ifstartis not given, use 0.
      • iter: Required. An iterable whose items must be numbers.
      • start: Optional. A number whose value is to be added to the sum of all items in the iterable. By default, it is 0.
    • sorted(iter[, key=func_name, reverse=False]): Sorts the iterableiterusing the sorting criteria specified by the functionfunc_nameand returns the sorted tuple. Ifreverseis True, sort the tuple in descending order; otherwise, sort the tuple in ascending order.
      • iter: Required. An iterable to be sorted.
      • key: Optional. A function that specifies the sorting criteria.
      • reverse: Optional. True or False. If it is True, sort the iterable in descending order.