Intensity Coding
  • HOME
  • TUTORIALS
    • Python
    • Natural Language Processing
  • ABOUT US
    • About Us
    • Contact Us
    • Privacy Policy
    • Terms and Conditions
    • Disclaimer

Python

Python Sets

Python
Python
  • Introduction To Python
  • Write Your First Python Program
  • Indentation And Comments in Python
  • Variables in Python
  • Data Types in Python
  • Booleans and Number in Python
  • Operators in Python
  • Mutable Vs Immutable Objects And Collection in Python
  • Python String
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Control Statements
  • Python Functions
  • Python Lambda Functions
  • Python Regular Expressions (RegEx)
  • Python JSON

IC_Python_Tutorial-12_Python_Sets.png

Python Sets¶

Introduction to Sets¶

  • A set is a built-in data structure in Python that allows storing multiple unique values in a single variable.
  • Sets are unordered, meaning the elements do not maintain a specific order.
  • They are immutable, meaning items cannot be changed after creation, but elements can be added or removed.
  • Duplicate values are not allowed, ensuring that each element appears only once.
  • Sets can be defined using curly brackets {} or the set() constructor.
  • The len() function returns the number of elements in a set.
  • Note: Python treats True and 1 as equivalent values, so adding both will retain only one.

Syntax:¶

my_set = {"item1", "item2", "item3"}
In [ ]:
# Creating a set with different intensity levels
intensity_levels = {"high", "medium", "low", "high", True, 1, 2}

# Display the set (duplicates are automatically removed)
print(intensity_levels)  # Output: {'high', 'medium', 'low', 1, 2}

# Checking the number of unique elements
print(len(intensity_levels))  # Output: 5
{'low', True, 2, 'medium', 'high'}
5

Checking Set Type¶

  • In Python, sets are internally recognized as objects of type set.
  • Use the type() function to verify if a variable is a set.
In [ ]:
# Define a set
myset = {"Intensity", "Coding", "Python"}

# Check the type of the set
print(type(myset))
# Output: <class 'set'>
<class 'set'>

Working with Different Data Types¶

In [ ]:
# Sets containing same data types
coding_set1 = {"Python", "Java", "C++"}  # Strings
coding_set2 = {10, 20, 30, 40}  # Integers
coding_set3 = {True, False, False, True}  # Boolean values

# Set with mixed data types
coding_mixed = {"intensity coding", 100, False, "AI", 3.14}

# Displaying the sets
print(coding_set1)  # Output: {'Python', 'Java', 'C++'}
print(coding_set2)  # Output: {10, 20, 30, 40}
print(coding_set3)  # Output: {False, True} (removes duplicates)
print(coding_mixed)  # Output: {'intensity coding', 100, False, 'AI', 3.14}
{'Java', 'C++', 'Python'}
{40, 10, 20, 30}
{False, True}
{False, 'intensity coding', 'AI', 3.14, 100}

Using the set() Constructor¶

  • Instead of curly braces {}, we can also use the set() constructor to create a set.
In [ ]:
# Creating a set using the set() function
tech_topics = set(("Machine Learning", "Deep Learning", "intensity coding"))

# Display the set
print(tech_topics)  # Output: {'Machine Learning', 'Deep Learning', 'intensity coding'}
{'Deep Learning', 'intensity coding', 'Machine Learning'}

Accessing Set Items¶

  • Unlike lists and tuples, sets do not support indexing or key-based access.
  • However, you can iterate over a set using a for loop.
  • You can also check if an element exists in a set using the in keyword.

Iterating Through a Set¶

In [ ]:
# Define a set of programming concepts
coding_topics = {"machine learning", "deep learning", "intensity coding"}

# Loop through the set
for topic in coding_topics:
    print(topic)
deep learning
intensity coding
machine learning

Checking for an Item in a Set¶

In [ ]:
# Define a set
coding_set = {"Python", "Java", "intensity coding"}

# Check if "intensity coding" exists in the set
print("intensity coding" in coding_set)  # Output: True
True

Modifying Set Items¶

  • Set elements cannot be modified after creation.

  • However, you can add new items or remove existing ones.

In [ ]:
# Define a set
tech_stack = {"Python", "TensorFlow", "PyTorch"}

# Add a new element
tech_stack.add("intensity coding")

# Display the updated set
print(tech_stack)  # Output: {'Python', 'TensorFlow', 'PyTorch', 'intensity coding'}
{'TensorFlow', 'intensity coding', 'PyTorch', 'Python'}

Adding Items to a Set¶

  • To add a single item to a set, use the add() method.
  • Since sets are unordered, the new element’s position is not guaranteed.
In [ ]:
# Define a set of AI topics
ai_topics = {"machine learning", "deep learning", "intensity coding"}

# Add a new topic to the set
ai_topics.add("neural networks")

# Display the updated set
print(ai_topics)  # Output: {'machine learning', 'deep learning', 'intensity coding', 'neural networks'}
{'deep learning', 'intensity coding', 'neural networks', 'machine learning'}

Adding Items from Another Sets¶

  • To merge another set into an existing set, use the update() method.

  • This method adds all elements from one set to another.

In [ ]:
# Define two sets
ai_set = {"Python", "TensorFlow", "intensity coding"}
new_tech = {"PyTorch", "OpenAI", "LLMs"}

# Add new_tech set into ai_set
ai_set.update(new_tech)

# Display the updated set
print(ai_set)  # Output: {'Python', 'TensorFlow', 'intensity coding', 'PyTorch', 'OpenAI', 'LLMs'}
{'intensity coding', 'PyTorch', 'LLMs', 'Python', 'OpenAI', 'TensorFlow'}

Adding Items from an Iterable¶

  • The update() method works with any iterable (e.g., lists, tuples, dictionaries).

  • This allows adding multiple elements at once.

In [ ]:
# Define a set
ml_stack = {"supervised learning", "unsupervised learning", "intensity coding"}

# Define a list of additional topics
new_topics = ["reinforcement learning", "transfer learning"]

# Add list elements to the set
ml_stack.update(new_topics)

# Display the updated set
print(ml_stack)
# Output: {'supervised learning', 'unsupervised learning', 'intensity coding', 'reinforcement learning', 'transfer learning'}
{'intensity coding', 'transfer learning', 'unsupervised learning', 'reinforcement learning', 'supervised learning'}

Removing Items from a Set¶

  • remove() – Removes a specific item.
    • Raises an error if the item is not found.
  • discard() – Removes a specific item.
    • Does not raise an error if the item is not found.
  • pop() – Removes a random item.
    • Since sets are unordered, you cannot predict which item will be removed.
    • Returns the removed item.
  • clear() – Empties the set, removing all elements.
  • del keyword – Deletes the set entirely.

Using remove()¶

  • The remove() method deletes a specific item, but if the item is not found, it raises an error.
In [ ]:
# Define a set
ai_frameworks = {"TensorFlow", "PyTorch", "intensity coding"}

# Remove an existing item
ai_frameworks.remove("PyTorch")

# Display the updated set
print(ai_frameworks)  # Output: {'TensorFlow', 'intensity coding'}
{'TensorFlow', 'intensity coding'}

Using discard()¶

  • The discard() method also removes a specific item, but it does not raise an error if the item does not exist
In [ ]:
# Define a set
ml_topics = {"classification", "regression", "intensity coding"}

# Attempt to remove an item (safe even if not found)
ml_topics.discard("clustering")

# Display the set after discard
print(ml_topics)  # Output: {'classification', 'regression', 'intensity coding'}
{'intensity coding', 'classification', 'regression'}

Using pop()¶

  • The pop() method removes a random item from the set. Since sets are unordered, you cannot predict which item will be removed
In [ ]:
# Define a set
data_types = {"structured", "unstructured", "semi-structured", "intensity coding"}

# Remove a random element
removed_item = data_types.pop()

# Display the removed item and the updated set
print("Removed item:", removed_item)
print("Remaining set:", data_types)
Removed item: intensity coding
Remaining set: {'semi-structured', 'structured', 'unstructured'}

Using clear()¶

  • The clear() method removes all elements from the set, leaving an empty set.
In [ ]:
# Define a set
ai_models = {"CNN", "RNN", "GAN", "intensity coding"}

# Clear all elements from the set
ai_models.clear()

# Display the empty set
print(ai_models)  # Output: set()
set()

Deleting a Set Using del¶

  • The del keyword completely removes a set from memory.
In [ ]:
# Define a set
frameworks = {"TensorFlow", "Keras", "intensity coding"}

# Delete the set
del frameworks

# Uncommenting the next line will cause an error because the set no longer exists
# print(frameworks)  # NameError: name 'frameworks' is not defined

Looping Through a Set¶

  • Since sets are unordered, their elements are stored in random order.
  • You can loop through a set using a for loop.
  • This allows processing each element in the set individually.
In [ ]:
# Define a set of AI concepts
ai_concepts = {"machine learning", "deep learning", "intensity coding"}

# Loop through the set and print each item
for concept in ai_concepts:
    print(concept)
deep learning
intensity coding
machine learning

Joining Sets¶

  • Python provides multiple ways to join two or more sets:
  1. union() – Returns a new set containing all unique elements from both sets.
  2. update() – Adds elements from one set to another, modifying the original set.
  3. intersection() & intersection_update() – Keep only the common elements.
  4. symmetric_difference() & symmetric_difference_update() – Keep elements that are unique to each set.

Joining Two Sets¶

  • The union() method returns a new set with all unique elements from both sets.
  • The update() method adds elements from one set into another, modifying the original set.
  • Duplicate elements are excluded in both methods.

Using union()¶

In [ ]:
# Define two sets
ai_topics = {"machine learning", "deep learning", "intensity coding"}
data_topics = {"big data", "data science", 1, 2}

# Create a new set with all unique elements
combined_topics = ai_topics.union(data_topics)

# Display the result
print(combined_topics)
# Output (order may vary): {'machine learning', 'deep learning', 'intensity coding', 'big data', 'data science', 1, 2}
{'deep learning', 'intensity coding', 1, 2, 'data science', 'big data', 'machine learning'}

Using update()¶

In [ ]:
# Define two sets
set1 = {"Python", "AI", "intensity coding"}
set2 = {"TensorFlow", "PyTorch", "NLP"}

# Update set1 with elements from set2
set1.update(set2)

# Display the modified set1
print(set1)
# Output (order may vary): {'Python', 'AI', 'intensity coding', 'TensorFlow', 'PyTorch', 'NLP'}
{'AI', 'intensity coding', 'PyTorch', 'NLP', 'Python', 'TensorFlow'}

Keeping Only the Common Elements¶

  • The intersection_update() method keeps only elements present in both sets, modifying the original set.
  • The intersection() method returns a new set containing only common elements.

Using intersection_update()¶

In [ ]:
# Define two sets
frameworks1 = {"TensorFlow", "PyTorch", "intensity coding"}
frameworks2 = {"PyTorch", "Keras", "intensity coding"}

# Keep only common elements
frameworks1.intersection_update(frameworks2)

# Display the updated set
print(frameworks1)  # Output: {'PyTorch', 'intensity coding'}
{'PyTorch', 'intensity coding'}

Using intersection()¶

In [ ]:
# Define two sets
setA = {"AI", "ML", "intensity coding"}
setB = {"ML", "Data Science", "intensity coding"}

# Create a new set with common elements
common_set = setA.intersection(setB)

# Display the result
print(common_set)  # Output: {'ML', 'intensity coding'}
{'ML', 'intensity coding'}

Keeping All, But Not the Common Elements¶

  • The symmetric_difference_update() method removes common elements, keeping only unique ones.
  • The symmetric_difference() method returns a new set with only unique elements from both sets.

Using symmetric_difference_update()¶

In [ ]:
# Define two sets
tech_set1 = {"Python", "AI", "intensity coding"}
tech_set2 = {"AI", "Blockchain", "intensity coding"}

# Keep only non-common elements
tech_set1.symmetric_difference_update(tech_set2)

# Display the updated set
print(tech_set1)  # Output (order may vary): {'Python', 'Blockchain'}
{'Blockchain', 'Python'}

Using symmetric_difference()¶

In [ ]:
# Define two sets
setX = {"machine learning", "deep learning", "intensity coding"}
setY = {"deep learning", "data analytics", "intensity coding"}

# Create a new set with only non-common elements
unique_set = setX.symmetric_difference(setY)

# Display the result
print(unique_set)
# Output (order may vary): {'machine learning', 'data analytics'}
{'data analytics', 'machine learning'}

Python Set Methods¶

No Method Description
1 add() Inserts an element into the set.
2 clear() Removes all elements, leaving the set empty.
3 copy() Creates and returns a duplicate of the set.
4 difference() Returns a new set containing elements that exist in the first set but not in others.
5 difference_update() Modifies the set by removing elements that are also found in another set.
6 discard() Eliminates the specified element if it exists; does nothing if the element is absent.
7 intersection() Generates a new set containing only elements that are common in multiple sets.
8 intersection_update() Updates the set, keeping only elements found in all specified sets.
9 isdisjoint() Checks whether two sets share no common elements, returning True or False.
10 issubset() Determines if the current set is completely contained within another set.
11 issuperset() Verifies whether the current set includes all elements of another set.
12 pop() Removes and returns an arbitrary element from the set.
13 remove() Deletes a specified element from the set; raises an error if the element is missing.
14 symmetric_difference() Returns a new set with elements unique to each set, excluding common elements.
15 symmetric_difference_update() Updates the set by keeping only elements that are not present in both sets.
16 union() Returns a set containing all elements from both sets, eliminating duplicates.
17 update() Merges another set into the current one, adding any new elements.

1. add()¶

Point Description
Use Case The add() method inserts a new element into a set. If the element already exists, the set remains unchanged.
Syntax set.add(element)
Parameter Values element : Required. The element to add to the set.

Useful Information¶

No. Point Description
1 No Duplicate Insertions If the element is already present in the set, it is not added again.
2 No Return Value The method modifies the set in place and does not return a new set.
3 Mutable but Unordered The insertion order is not preserved in sets.
In [ ]:
# Creating a set of programming topics covered on Intensity Coding
topics = {"Machine Learning", "Deep Learning", "NLP"}

# Adding a new topic to the set
topics.add("Generative AI")
print(topics)  # Output: {'Machine Learning', 'Deep Learning', 'NLP', 'Generative AI'}

# Trying to add an existing topic
topics.add("Machine Learning")

# The set remains unchanged as duplicates are not allowed
print(topics)  # Output: {'Machine Learning', 'Deep Learning', 'NLP', 'Generative AI'}
{'Generative AI', 'Deep Learning', 'NLP', 'Machine Learning'}
{'Generative AI', 'Deep Learning', 'NLP', 'Machine Learning'}

2. clear()¶

Point Description
Use Case The clear() method removes all elements from a set, making it empty.
Syntax set.clear()
Parameter Values This method does not accept any parameters.

Useful Information¶

No. Point Description
1 Removes All Elements The method clears all elements but retains the set object itself.
2 No Return Value The method modifies the original set and does not return a new set.
3 Useful for Resetting Sets Often used to reset a set instead of creating a new one.
In [ ]:
# Creating a set of AI topics on Intensity Coding
topics = {"Machine Learning", "Deep Learning", "NLP", "Generative AI"}

# Clearing all topics
topics.clear()

# Printing the empty set
print(topics)  # Output: set()
set()

3. copy()¶

Point Description
Use Case The copy() method creates a shallow copy of a set, preserving its elements b.ut creating a new set object.
Syntax set.copy()
Parameter Values This method does not accept any parameters.

Useful Information¶

No. Point Description
1 Creates a New Set The copied set is independent of the original set.
2 Shallow Copy The elements themselves are not copied, only references to them.
3 Original Set Unaffected Modifying the copied set does not affect the original set.
In [ ]:
# Creating a set of AI topics
topics = {"Machine Learning", "Deep Learning", "NLP", "Generative AI"}

# Copying the set
topics_copy = topics.copy()

# Printing the copied set
print(topics_copy)  # Output: {'Machine Learning', 'Deep Learning', 'NLP', 'Generative AI'}

# Modifying the copied set
topics_copy.add("Computer Vision")

# Checking that the original set remains unchanged
print("Original Set:", topics)       # Output: {'Machine Learning', 'Deep Learning', 'NLP', 'Generative AI'}
print("Copied Set:", topics_copy)    # Output: {'Machine Learning', 'Deep Learning', 'NLP', 'Generative AI', 'Computer Vision'}
{'Deep Learning', 'Generative AI', 'NLP', 'Machine Learning'}
Original Set: {'Deep Learning', 'Generative AI', 'NLP', 'Machine Learning'}
Copied Set: {'NLP', 'Computer Vision', 'Deep Learning', 'Generative AI', 'Machine Learning'}

4. difference()¶

Point Description
Use Case The difference() method returns a new set containing elements that are in the first set but not in the second set.
Syntax set1.difference(set2)
Parameter Values set2: Required. The set whose elements are excluded from the result.

Useful Information¶

No. Point Description
1 Returns a New Set The original sets remain unchanged.
2 Order Not Preserved The returned set does not maintain any specific order.
3 Asymmetric Operation A.difference(B) is not necessarily equal to B.difference(A).
In [ ]:
# Topics covered on Intensity Coding
intensity_coding_topics = {"Machine Learning", "Deep Learning", "NLP", "Generative AI"}

# Topics covered on another platform
other_platform_topics = {"Machine Learning", "Deep Learning", "Computer Vision"}

# Finding topics unique to Intensity Coding
unique_topics = intensity_coding_topics.difference(other_platform_topics)
print(unique_topics)
# Output: {'NLP', 'Generative AI'}

# Finding topics unique to the other platform
unique_other_topics = other_platform_topics.difference(intensity_coding_topics)
print(unique_other_topics)
# Output: {'Computer Vision'}
{'Generative AI', 'NLP'}
{'Computer Vision'}

5. difference_update()¶

Point Description
Use Case The difference_update() method removes elements from the first set that are also present in the second set. Unlike difference(), this method modifies the original set instead of returning a new one.
Syntax set1.difference_update(set2)
Parameter Values set2: Required. The set whose common elements will be removed from set1.

Useful Information¶

No. Point Description
1 Modifies the Original Set Unlike difference(), it directly updates the calling set.
2 No Return Value The method does not return a new set; it modifies set1 in place.
3 Removes Common Elements Elements that exist in both sets are deleted from set1.
In [ ]:
# Topics on Intensity Coding
intensity_coding_topics = {"Machine Learning", "Deep Learning", "NLP", "Generative AI"}

# Topics covered on another platform
other_platform_topics = {"Machine Learning", "Deep Learning", "Computer Vision"}

# Removing topics that exist on both platforms
intensity_coding_topics.difference_update(other_platform_topics)

print(intensity_coding_topics)
# Output: {'NLP', 'Generative AI'}
{'Generative AI', 'NLP'}

6. discard()¶

Point Description
Use Case The discard() method removes a specified element from a set. If the element is not found, the method does not raise an error.
Syntax set.discard(element)
Parameter Values element: Required. The item to remove from the set.

Useful Information¶

No. Point Description
1 No Error if Element Not Found Unlike remove(), it does not raise an error if the element is missing.
2 No Return Value The method modifies the original set and returns None.
3 Order is Not Maintained Since sets are unordered, removal does not affect element positions.
In [ ]:
# Topics on Intensity Coding
intensity_coding_topics = {"Machine Learning", "Deep Learning", "NLP", "Generative AI"}

# Removing "NLP" from the set
intensity_coding_topics.discard("NLP")

print(intensity_coding_topics)
# Output: {'Machine Learning', 'Deep Learning', 'Generative AI'}

# Attempting to remove a topic that does not exist
intensity_coding_topics.discard("Computer Vision")

# No error occurs, and the set remains unchanged
print(intensity_coding_topics)
# Output: {'Machine Learning', 'Deep Learning', 'Generative AI'}
{'Generative AI', 'Deep Learning', 'Machine Learning'}
{'Generative AI', 'Deep Learning', 'Machine Learning'}

7. intersection()¶

Point Description
Use Case The intersection() method returns a set containing only the elements that are common between two or more sets.
Syntax set.intersection(set1, set2, ...)
Parameter Values set1: Required. The first set to compare.
set2, ... : Optional. Additional sets to compare. Multiple sets can be provided, separated by commas.

Useful Information¶

No. Point Description
1 Returns a New Set The original sets remain unchanged, and a new set with common elements is returned.
2 Works with Multiple Sets Can compare two or more sets to find shared elements.
3 Order is Not Maintained Since sets are unordered, the order of elements in the result is arbitrary.
In [ ]:
# AI topics discussed on different forums
intensity_coding_topics = {"Machine Learning", "Deep Learning", "NLP", "Generative AI"}
research_paper_topics = {"Deep Learning", "Computer Vision", "Generative AI"}
industry_trends = {"NLP", "Generative AI", "Edge AI"}

# Finding topics common between Intensity Coding and research papers
common_with_research = intensity_coding_topics.intersection(research_paper_topics)
print(common_with_research)
# Output: {'Deep Learning', 'Generative AI'}

# Finding topics common in all three domains
common_all = intensity_coding_topics.intersection(research_paper_topics, industry_trends)
print(common_all)
# Output: {'Generative AI'}
{'Generative AI', 'Deep Learning'}
{'Generative AI'}

8. intersection_update()¶

Point Description
Use Case The intersection_update() method removes elements from the original set that are not present in another set or multiple sets.
Syntax set.intersection_update(set1, set2, ...)
Parameter Values set1 : Required. The first set to compare.
set2, ... Optional. Additional sets to compare. Multiple sets can be provided, separated by commas.

Useful Information¶

No. Point Description
1 Modifies the Original Set Unlike intersection(), this method modifies the original set instead of returning a new one.
2 Works with Multiple Sets It updates the original set with only the elements common across all given sets.
3 Removes Uncommon Elements Any element that is not found in all compared sets is removed from the original set.
In [ ]:
# AI topics covered on Intensity Coding
intensity_coding_topics = {"Machine Learning", "Deep Learning", "NLP", "Generative AI"}

# AI topics discussed in research papers
research_paper_topics = {"Deep Learning", "Computer Vision", "Generative AI"}

# Keep only the topics that are present in both sets
intensity_coding_topics.intersection_update(research_paper_topics)
print(intensity_coding_topics)
# Output: {'Deep Learning', 'Generative AI'}

# Another example with three sets
x = {"A", "B", "C"}
y = {"B", "C", "D"}
z = {"C", "E", "F"}

x.intersection_update(y, z)
print(x)
# Output: {'C'}
{'Generative AI', 'Deep Learning'}
{'C'}

9. isdisjoint()¶

Point Description
Use Case The isdisjoint() method checks whether two sets have no common elements. If the sets have no intersection, it returns True; otherwise, it returns False.
Syntax set.isdisjoint(other_set)
Parameter Values other_set : Required. The set to compare for common elements.

Useful Information¶

No. Point Description
1 Returns a Boolean The method returns True if the sets have no common elements and False otherwise.
2 Does Not Modify Sets This method does not alter the original sets.
3 Works with Multiple Data Types The sets can contain different types of elements (strings, numbers, etc.).
In [ ]:
# AI topics covered on Intensity Coding
intensity_coding_topics = {"Machine Learning", "Deep Learning", "NLP", "Generative AI"}

# Topics covered in a cybersecurity blog
cybersecurity_topics = {"Network Security", "Ethical Hacking", "Malware Analysis"}

# Check if there is no overlap
no_overlap = intensity_coding_topics.isdisjoint(cybersecurity_topics)
print(no_overlap)
# Output: True (No common topics)

# Another example where sets share an element
other_ai_topics = {"Computer Vision", "NLP", "AI Ethics"}

# Check if there is no overlap
has_common_topic = intensity_coding_topics.isdisjoint(other_ai_topics)
print(has_common_topic)
# Output: False (Both sets contain "NLP")
True
False

10. issubset()¶

Point Description
Use Case The issubset() method checks whether all elements of a set exist in another set. It returns True if the set is a subset, otherwise False.
Syntax set.issubset(other_set)
Parameter Values other_set : Required. The set to compare against.

Useful Information¶

No. Point Description
1 Returns a Boolean Returns True if the first set is a subset of the second set, otherwise False.
2 Does Not Modify Sets This method does not change the original sets.
3 Works with Multiple Data Types The elements in the sets can be strings, numbers, or other hashable data types.
In [ ]:
# Core AI topics covered on Intensity Coding
core_topics = {"Machine Learning", "Deep Learning", "NLP"}

# All topics covered including additional AI subjects
all_topics = {"Machine Learning", "Deep Learning", "NLP", "Computer Vision", "Generative AI"}

# Check if all core topics are part of the full topic list
is_subset = core_topics.issubset(all_topics)
print(is_subset)
# Output: True (All core topics exist in the full topic set)

# Example where a set is not a subset
random_topics = {"Blockchain", "NLP", "Quantum Computing"}

# Check if random topics are a subset of AI topics
is_subset = random_topics.issubset(all_topics)
print(is_subset)
# Output: False (Not all elements exist in all_topics)
True
False

11. issuperset()¶

Point Description
Use Case The issuperset() method checks whether all elements of another set exist in the current set. It returns True if the current set is a superset, otherwise False.
Syntax set.issuperset(other_set)
Parameter Values other_set : Required. The set to compare against.

Useful Information¶

No. Point Description
1 Returns a Boolean Returns True if the first set contains all elements of the second set, otherwise False.
2 Does Not Modify Sets This method does not change the original sets.
3 Works with Multiple Data Types The elements in the sets can be strings, numbers, or other hashable data types.
In [ ]:
# All AI topics covered by Intensity Coding
intensity_coding_topics = {"Machine Learning", "Deep Learning", "NLP", "Computer Vision", "Generative AI"}

# Core AI topics
core_topics = {"Machine Learning", "Deep Learning", "NLP"}

# Check if Intensity Coding covers all core topics
is_superset = intensity_coding_topics.issuperset(core_topics)
print(is_superset)
# Output: True (All core topics are covered by Intensity Coding)

# Example where a set is not a superset
random_topics = {"NLP", "Quantum Computing"}

# Check if Intensity Coding covers all random topics
is_superset = intensity_coding_topics.issuperset(random_topics)
print(is_superset)
# Output: False (Not all random topics exist in intensity_coding_topics)
True
False

12. pop()¶

Point Description
Use Case The pop() method removes and returns a random item from the set. Since sets are unordered, the removed item is not guaranteed to be the first or last element.
Syntax set.pop()
Parameter Values This method does not take any parameters.

Useful Information¶

No. Point Description
1 Removes a Random Item The item removed is selected randomly because sets are unordered.
2 Returns the Removed Item The method returns the element that was removed.
3 Raises an Error on Empty Set If the set is empty, calling pop() results in a KeyError.
In [ ]:
# Set of AI topics covered by Intensity Coding
ai_topics = {"Machine Learning", "Deep Learning", "NLP", "Computer Vision", "Generative AI"}

# Remove a random topic
removed_topic = ai_topics.pop()
print("Removed Topic:", removed_topic)

# Display the remaining topics
print("Remaining Topics:", ai_topics)

# Output (varies due to randomness)
#Removed Topic: Deep Learning
#Remaining Topics: {'Machine Learning', 'NLP', 'Computer Vision', 'Generative AI'}
Removed Topic: Computer Vision
Remaining Topics: {'Generative AI', 'NLP', 'Deep Learning', 'Machine Learning'}

13. remove()¶

Point Description
Use Case The remove() method deletes a specified element from the set. If the element does not exist, it raises a KeyError.
Syntax set.remove(item)
Parameter Values item : Required. The element to remove from the set.

Useful Information¶

No. Point Description
1 Removes a Specific Item Unlike pop(), which removes a random item, remove() targets a specific element.
2 Raises an Error if the Item is Missing If the specified item does not exist in the set, a KeyError occurs.
3 Use discard() to Avoid Errors If you want to remove an item without raising an error, use discard() instead.
In [ ]:
# Set of AI topics covered by Intensity Coding
ai_topics = {"Machine Learning", "Deep Learning", "NLP", "Computer Vision", "Generative AI"}

# Remove a specific topic
ai_topics.remove("NLP")
print("Updated Topics:", ai_topics)

# Trying to remove a non-existing topic (This will raise an error)
#ai_topics.remove("Reinforcement Learning")  # KeyError: 'Reinforcement Learning'
Updated Topics: {'Computer Vision', 'Generative AI', 'Deep Learning', 'Machine Learning'}

14. symmetric_difference()¶

Point Description
Use Case The symmetric_difference() method returns a set containing elements that are in either of the two sets but not in both.
Syntax set.symmetric_difference(other_set)
Parameter Values other_set : Required. The set to compare against.

Useful Information¶

No. Point Description
1 Excludes Common Elements The returned set contains elements unique to each set.
2 Does Not Modify Original Sets Unlike symmetric_difference_update(), this method returns a new set without modifying the original sets.
3 Equivalent to XOR Operation in Sets Can also be performed using the ^ operator: set1 ^ set2.
In [ ]:
# AI topics covered by Intensity Coding
intensity_topics = {"Machine Learning", "Deep Learning", "NLP", "Generative AI"}

# AI topics covered by another platform
other_platform_topics = {"Machine Learning", "Computer Vision", "NLP", "Robotics"}

# Find unique topics in both platforms
unique_topics = intensity_topics.symmetric_difference(other_platform_topics)
print("Unique AI Topics:", unique_topics) #Output: Unique AI Topics: {'Deep Learning', 'Generative AI', 'Robotics', 'Computer Vision'}
Unique AI Topics: {'Robotics', 'Computer Vision', 'Generative AI', 'Deep Learning'}

15. symmetric_difference_update()¶

Point Description
Use Case The symmetric_difference_update() method updates the original set by removing elements that exist in both sets and adding elements that are unique to either set.
Syntax set.symmetric_difference_update(other_set)
Parameter Values other_set : Required. The set to compare against.

Useful Information¶

No. Point Description
1 Modifies the Original Set Unlike symmetric_difference(), this method does not return a new set but updates the existing one.
2 Removes Common Elements Items that appear in both sets are removed.
3 Equivalent to XOR Assignment (^=) Can also be performed using the ^= operator: set1 ^= set2.
In [ ]:
# AI topics covered by Intensity Coding
intensity_topics = {"Machine Learning", "Deep Learning", "NLP", "Generative AI"}

# AI topics covered by another platform
other_platform_topics = {"Machine Learning", "Computer Vision", "NLP", "Robotics"}

# Update Intensity Coding's topics to include unique topics while removing common ones
intensity_topics.symmetric_difference_update(other_platform_topics)

print("Updated AI Topics:", intensity_topics) #Output: Updated AI Topics: {'Deep Learning', 'Generative AI', 'Robotics', 'Computer Vision'}
Updated AI Topics: {'Robotics', 'Computer Vision', 'Generative AI', 'Deep Learning'}

16. union()¶

Point Description
Use Case The union() method creates a new set containing all unique elements from the original set and one or more specified sets (or iterables).
Syntax set.union(set1, set2, ...)
Parameter Values set1, set2, ... : Required. The sets or iterables to combine.

Useful Information¶

No. Point Description
1 No Duplicates If an item appears in multiple sets, it is included only once in the result.
2 Works with Iterables You can use lists, tuples, or other iterables instead of sets.
3 Does Not Modify Original Set Unlike update(), union() returns a new set and does not alter the original sets.
In [ ]:
# AI topics covered by Intensity Coding
intensity_topics = {"Machine Learning", "Deep Learning", "NLP"}

# AI topics covered by another platform
other_platform_topics = {"Computer Vision", "NLP", "Generative AI"}

# Topics covered by a research institute
research_topics = {"Reinforcement Learning", "Machine Learning", "Robotics"}

# Creating a union of all AI topics
all_topics = intensity_topics.union(other_platform_topics, research_topics)

print("Combined AI Topics:", all_topics) #Output: Combined AI Topics: {'Machine Learning', 'Deep Learning', 'NLP', 'Computer Vision', 'Generative AI', 'Reinforcement Learning', 'Robotics'}
Combined AI Topics: {'Computer Vision', 'Deep Learning', 'NLP', 'Machine Learning', 'Generative AI', 'Robotics', 'Reinforcement Learning'}

17. update()¶

Point Description
Use Case The update() method modifies the original set by adding all elements from another set or iterable.
Syntax set.update(iterable)
Parameter Values iterable : Required. The set, list, tuple, or any iterable whose elements will be added.

Useful Information¶

No. Point Description
1 Modifies Original Set Unlike union(), which creates a new set, update() changes the original set.
2 No Duplicates If an item exists in both sets, it is only added once.
3 Works with Iterables Accepts lists, tuples, and other iterable types in addition to sets.
In [ ]:
# Existing AI topics on Intensity Coding
intensity_topics = {"Machine Learning", "Deep Learning", "NLP"}

# New topics to be added from other platforms
new_topics = {"Generative AI", "Computer Vision", "NLP"}  # 'NLP' is duplicate

# Updating the set with new topics
intensity_topics.update(new_topics)

print("Updated AI Topics:", intensity_topics) #Output: Updated AI Topics: {'Machine Learning', 'Deep Learning', 'NLP', 'Generative AI', 'Computer Vision'}
Updated AI Topics: {'Computer Vision', 'Deep Learning', 'NLP', 'Machine Learning', 'Generative AI'}
Category
Python Natural Language Processing
Tag's
Python Natural Language Processing

Quick Links

  • Home
  • Tutorials
  • About Us
  • Contact Us

Tutorials

  • Python
  • Natural Language Processing

Tag's

  • Python
  • Natural Language Processing

About Intensity Coding

  • Intensity Coding is your learning hub for Data Science, Machine Learning, Deep Learning, NLP, and Generative AI. From beginner to expert level, we offer clear tutorials, real-world examples, and up-to-date insights. Our content simplifies complex topics to help you grow in your AI journey.
Intensity Coding

© 2025 Intensity Coding. All Rights Reserved.

Privacy Policy Terms and Conditions Disclaimer