Python

Sets In Python – Detailed Answers From [A-Z]

In Python, a set is an unindexed, unordered collection of things. Unordered indicates that the items of a set will appear in a random order when displayed. Unindexed signifies that, unlike lists and tuples, a set’s elements cannot be accessed using the indexes.

A set’s components are listed inside curly brackets and set components are separated by commas. For instance –

myset = {1, 2, 3, 4, "hello"}

Python Set Example

# Set Example
myset = {"hi", 2, "bye", "Hello World"}
print(myset)

Output:
Python set example

Determining whether a piece is part of the set

Using the “in” operator, we may determine whether an item is included in a Set or not, as demonstrated in the example below. Whether true or false is returned as a boolean value. It returns true if the supplied set contains the item; otherwise, it returns false.

# Set Example
myset = {"hi", 2, "bye", "Hello World"}

# checking whether 2 is in myset
print(2 in myset)

# checking whether "hi" is in myset
print("hi" in myset)

# checking whether "BeginnersBook" is in myset
print("BeginnersBook" in myset)

Output:
Python checking an item in the Set

Python allows you to iterate through a Set’s items.

As demonstrated in the following elements, we can iterate through a set’s elements in Python. Every time you run the code, the elements will display in the output in a different order, as you can see.

# Set Example
myset = {"hi", 2, "bye", "Hello World"}

# loop through the elements of myset
for a in myset:
    print(a)

Output:
Python set loop

Add or remove items from a set using Python

As demonstrated in the following example, we can add an item to a Set using the add() function and remove an item from a Set using the remove() function.

# Set Example
myset = {"hi", 2, "bye", "Hello World"}
print("Original Set:", myset)

# adding an item
myset.add(99)
print("Set after adding 99:", myset)

# removing an item
myset.remove("bye")
print("Set after removing bye:", myset)

Output:
Python add or remove item from a Set

Set Procedures

  • 1. add(): This function expands the Set by one element.
  • 2. The remove() method eliminates a particular element from the Set.
  • 3. Discard(): This function performs similarly to remove(), but it doesn’t throw an exception when the requested element is absent.
  • 4. clear() eliminates all of the set’s components.
  • 5. The shallow copy of the set is returned by copy().
  • 6. difference(): This method creates a new set by comparing the two sets that are supplied.
  • 7. difference update(): This function updates the calling set with the differences of the two sets that are sent in.
  • 8. intersection(): This function creates a new set from all the sets and includes their shared items.
  • 9. intersection update(): This function updates the calling set with the intersection of the two sets provided.
  • 10. isdisjoint() determines whether or not two sets are disjoint. If there are no shared elements between two sets, they are disjoint.
  • 11. The issubset() function determines whether a given set is a subset of another set.
  • 12. pop(): Removes a random element from the set and returns it.
  • 13. union(): Returns a new set containing all of the sets’ unique members.
  • 14. update(): Adds items to a set from another iterable that has been supplied.
  • 15. symmetric difference(): Produces a new set that is the symmetric difference of the two sets that are supplied.
  • 16. symmetric difference update(): This function updates the called set with the symmetric difference of the two sets.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button