View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
    View All

    Set in Python

    Updated on 11/10/20242,942 Views

    Introduction

    Python is known for its flexibility and popularity, offering various tools to simplify coding. Among these tools, sets are useful for managing groups of items. We will detail sets in Python, looking at their characteristics, construction, manipulation, and numerous operations. Starting with the basics and progressing to advanced concepts, we'll cover creating sets, manipulating them, and comparing them. By the end of this tutorial, you'll gain a firm understanding of how sets in Python function.

    Overview:

    A set in Python is an unordered collection of unique elements that doesn't allow duplicates. Sets come in handy when dealing with distinct items. They're mutable, so you can add or remove elements after creation. The related data structure frozenset in Python resembles a set but is immutable.

    What is a Set?

    In Python, a set is a collection of unique elements, perfect for handling items without duplicates. Sets are represented with curly braces {} and commas separate elements.

    Creating a Set or set() function in Python:
    In Python, you can create sets using {}. Here's an example:

    fruits = {"apple", "banana", "orange", "apple", "pear"}
    print(fruits)

    Here, "apple" is duplicated, but the set will only contain one instance. When you print 'fruits', duplicates are automatically removed.

    Output:

    Basic Operations:
    Sets support various operations like adding, removing, and checking membership.

    # Creating a set
    my_set = {1, 2, 3, 4, 5,}
    # Adding an element
    my_set.add(6)
    # Removing an element
    my_set.remove(3)
    # Checking membership
    print(2 in my_set) # Output: True
    print(3 in my_set) # Output: False

    Set Operations:
    Sets also allow for mathematical operations like union, intersection, and difference.

    set_A = {1, 2, 3, 4}
    set B = [3, 4, 5, 6]
    # Union of sets A and B
    union = set_A | set_B
    # Intersection of sets A and B
    intersection = set_A & set_B
    # Difference between sets A and B
    difference = set_A set_B
    print(union)    # Output: {1, 2, 3, 4, 5, 6}
    print(intersection)   # Output: {3, 4}
    print(difference)    # Output: {1, 2}

    Use Case Example:
    Consider a practical scenario: you have a shopping cart with a list of items, and you want to eliminate duplicates before proceeding to checkout.

    shopping_cart = ["apple", "banana", "apple", "orange", "banana", "pear"]
    unique_items = set(shopping_cart)
    print(unique_items)

    Output:

    Set in Python is Mutable or Immutable?

    Sets in Python are mutable, allowing the addition, removal, and modification of elements. Unlike immutable data types, sets can be changed after creation, making them suitable for dynamic collections. However, their mutability should be considered when shared across code sections or in concurrent programming.

    Set Items

    In Python, sets store unique items, prohibiting duplicates. They can hold various data types, enclosed in {} with items separated by commas.

    Creating Set Items:
    You can create a set using curly braces like this:

    my_set = [1, 2, 3, 4 3 5)
    print(my set)

    In this example, 'my_set' holds numbers 1 to 5.

    Checking Membership:
    To check if an item is in a set, use the 'in' keyword.

    numbers = {1, 2, 3, 4, 5}
    print(3 in numbers)   # Output: True
    print(6 in numbers)   # Output: False

    Iterating Through Set Items:
    Use loops to iterate through the items in a set:

    fruits = {apple", "banana", "orange"}
    for fruit in fruits:
         print(fruit)

    Example with Mixed Data Types:
    Sets can contain items of different types:

    mixed_set = {"apple", 123, 3.14, (1, 2, 3)}
    print(mixed set)

    In this case, the set ‘mixed_set’ contains a string, an integer, a float, and a tuple.

    Output:

    Converting List to Set:
    You can convert a list into a set to remove duplicate items:

    my_list = [1, 2, 2, 3, 3, 4, 5] 
    unique_set = set (my_list)
    print(unique_set)

    Create 'my_list' with duplicates and convert it to 'unique_set'. Python sets automatically remove duplicates, resulting in a set with only unique elements. Here's the output:

    Dictionary in Python

    A dictionary in Python is an unordered collection of key-value pairs. It allows you to store and retrieve data using unique keys. Keys are used to identify and access their corresponding values. Here's an example:

    # Creating a dictionary
    my_dict = {
           'name': 'Alice',
           'age': 30,
           'city': 'New York'
    }
    # Accessing values using keys
    print("Name:", my_dict['name'])
    print("Age:", my_dict['age'])
    print("City:", my_dict['city'])

    Output:

    How to Modify a Set in Python?

    Python set contains methods like add (), clear (), remove () and discard (). Sets can be modified using these.

    Adding Elements:
    Add elements to a set using the add() method. If the element is already in the set, it won't be duplicated because sets reject duplicates.

    fruits (apple", "banana", "orange")
    fruits.add("grape")
    print (fruits) # Output: {'orange', 'banana', 'apple', 'grape'}
    #Adding a duplicate element
    fruits.add("banana")
    print (fruits) # Output: ('orange', 'banana', 'apple', 'grape'}

    Removing Elements:
    Remove elements from a set using the remove() method: It removes an element if present but raises a KeyError if not found in the set.

    my_set = {1, 2, 3}
    my set.remove(2)   # Removes 2 from the set
    try:
         my_set.remove(4) # Element 4 is not present, raises KeyError
    except KeyError as e:
         print("KeyError:", e)
    print(my set)    # Output: (1, 3)

    Discarding Elements:

    To remove an element from a set without causing an error if it's absent, employ the 'discard()' method.

    my_set = {1, 2, 3}
    my set.discard (2) # Removes 2 from the set
    my_set.discard (4) # Element 4 is not present, no error
    print(my set) # Output: (1, 3)

    Clearing a Set:
    To empty a set and remove all its elements, use the 'clear()' method.

    fruits = {"apple", "banana', 'orange"}
    fruits.clear()
    print (fruits)    # Output: set()

    How to Get the Length of a Set?

    In Python, you can find a set's length (number of elements) with the 'len()' function.

    Example.

    fruits = {"apple", "banana", "orange", "pear"}
    length = len(fruits)
    print("The length of the 'fruits' set is:", length)

    Here, the len() function is applied to the fruits set, and the result is stored in the variable length. It displays the length of the set.

    Output:

    The value 4 indicates that the 'fruits' set contains four unique elements.

    Empty Set Length:
    If you apply the len() function to an empty set, the result will be 0.

    empty_set = set()
    length empty = len(empty_set)
    print("The length of the 'empty_set' is: ", length_empty)

    Output:

    Using Length in Conditional Statements:
    You can employ a set's length in conditional statements to verify if it's empty or has elements.

    my_set = {1, 2, 3, 4, 5}
    if len(my_set) > 0: 
           print("The set is not empty.")
    else:
           print("The set is empty.")

    Output:

    Type Casting with Python Set Method

    In Python, you can perform type casting to change data types. For sets, use the 'set()' constructor to create a set from iterable types like lists, tuples, and strings.

    Type Casting from List to Set:
    Convert a list to a set using 'set()'. Duplicates are automatically removed, leaving only unique elements.

    my list [1, 2, 2, 3, 3, 4, 5]
    my set set (my_list)
    print("Original list:", my_list)
    print("Set from list: ", my.set)

    Output:

    Type Casting from Tuple to Set:
    Similarly convert a tuple to a set using the set() constructor.

    my_tuple = (5, 6, 7, 7, 8)
    my set = set (my_tuple)
    print("Original tuple:", my_tuple) 
    print("Set from tuple:", my set)

    Output:

    Type Casting from String to Set:
    Convert a string to a set, where each character becomes an element in the set.

    my_string = "hello"
    my set = set (my_string)
    print("Original string:", my_string) 
    print("Set from string: ", my_set)

    Output:

    Type Casting from Dictionary Keys to Set:
    When you convert dictionary keys to a set, you get a set containing the unique keys.

    my_dict = {'a': 1, 'b': 2, 'c': 3}
    key_set = set(my_dict)
    print("Original dictionary: ", my_dict)
    print("Set from dictionary keys: ", key_set)

    Output:

    Check Unique and Immutable with Python Set

    Uniqueness in Sets:
    Sets in Python inherently enforce uniqueness. This feature makes sets useful for handling collections of distinct items.

    Example:

    # Creating a set with duplicate elements
    my_set = {1, 2, 2, 3, 3, 4, 5}
    print("Original set:", my_set)

    Output:

    Immutability of Set Elements:
    Sets are mutable, allowing element additions and removals. However, elements within a set must be of immutable data types, ensuring reliability for indexing and hashing, which is critical for set operations.
    Example:

    # Creating a set with different data types
    my_set = {1, 3.14, "hello", (2, 4)}
    # Attempting to add a mutable list to the set
    try:
        my_set.add([5, 6])
    except TypeError as e:
        print("TypeError:", e)

    Output:

    Here, 'my_set' comprises immutable elements like integers, floats, strings, and tuples. However, trying to add the mutable list [5, 6] to the set results in a 'TypeError' because lists, being mutable, cannot be used as set elements.

    Heterogeneous Element with Python Set

    In Python sets, you can combine elements of different data types, creating "heterogeneous" sets.

    Example:

    # Creating a set with heterogeneous elements 
    my_set = {1, 3.14, "hello", (2, 4)}
    print("My set:", my_set)

    Output:

    In this example, the set ‘my_set’ contains elements of various data types:

    • Integer (1)
    • Float (3.14)
    • String ("hello")
    • Tuple ((2, 4))

    Python Frozen Sets

    A frozen set in Python is an immutable type of set, where elements can't be altered after creation. This trait is handy for creating unchangeable collections of unique items.

    Creating a Frozen Set:
    Create a frozen set using 'frozenset()' by passing an iterable (e.g., list, tuple, or another set) as an argument.

    my_list = [1, 2, 3, 3, 4, 5]
    my_frozen_set = frozenset (my_list) 
    print("My frozen set:", my_frozen_set)

    Output:

    Frozen sets, once created, you can't add, remove, or modify their elements, ensuring their contents stay constant.

    my_frozen_set.add(6)    # This will raise an AttributeError

    Internal Working of Set

    Python sets use hash tables, offering fast average-case time complexity for actions like addition, removal, and membership checks.

    Hashing:
    Hashing transforms an element into a fixed-size value (hash code) representing it. Strings, integers, and tuples in Python are hashable and usable as keys in dictionaries and elements in sets.

    # Hash codes of integers
    hash_code_1 = hash(42)
    hash_code_2 = hash (99)
    print(hash_code_1, hash_code_2)

    The 'hash()' function in Python provides an object's hash value, which can vary across program runs. Typically, for integers, the hash value matches the integer value.

    Output:

    Here, 'hash_code_1' will equal the hash value of '42', and 'hash_code_2' will equal the hash value of '99', which corresponds to the integer values.

    Hash Tables:
    A hash table stores elements with their hash codes in an array of "buckets" determined by the hash codes.

    # Creating a simple hash table (illustration) 
    hash_table = [None] * 10 # Initialize with empty slots
    # Adding elements to hash table
    element_1 = "apple".
    element_2 = "banana"
    hash_code_1 = hash (element_1) 
    hash_code_2 = hash (element_2)
    bucket_1 = hash_code_1 % len (hash_table) 
    bucket_2 = hash_code_2% len (hash_table)
    hash_table[bucket_1] = element_1
    hash_table[bucket_2] = element_2
    print (hash_table)

    Output:

    Explanation:

    • The 'hash_table' is initialized with 10 empty slots, represented as '[None] * 10'.
    • Two elements, "apple" and "banana," are added to the hash table.
    • The hash codes for these elements are calculated using the 'hash()' function.
    • The hash codes are then mapped to buckets in the hash table using the modulo operation '%' with the length of the hash table.
    • "banana" is stored in the bucket at index 'hash_code_2 % len(hash_table)', which is index 4.
    • "apple" is stored in the bucket at index 'hash_code_1 % len(hash_table)', which is index 5.
    • The resulting 'hash_table' shows the placement of these elements in their respective buckets.

    Collision Resolution (Chaining):
    Collisions happen when two elements share the same hash code. Python manages collisions with chaining, where each bucket holds a linked list of elements with the same hash code.

    #Chaining (illustration)
    bucket with collision = 3
    linked_list = ["apple", "banana"]
    hash_table[bucket_with_collision] = linked_list
    print(hash_table)

    Internal Working of Sets:
    Python's set functions as a hash table implementation. When you create a set, Python employs the hash codes of elements to put them in suitable hash table buckets, enabling rapid retrieval and efficient membership checks.

    # Creating a set and adding elements 
    fruits = {"apple", "banana", "orange"}
    #Hash codes and buckets (illustration)
    hash_code_apple = hash("apple").
    hash_code_banana = hash("banana")
    hash_code_orange = hash("orange")
    bucket_apple = hash_code_apple % len (hash_table)
    bucket_banana = hash_code_banana % len(hash_table)
    bucket_orange =hash_code_orange % len (hash_table)
    print("Hash codes: ", hash_code_apple, hash_code_banana, hash_code_orange) print("Buckets: ", bucket_apple, bucket_banana, bucket_orange)

    This calculates hash codes for "apple," "banana," and "orange" elements and determines the hash table buckets where they'd be placed.

    Output:

    Explanation:

    • The 'hash()' function is used to calculate hash codes for the strings "apple," "banana," and "orange."
    • The modulo operation % is then applied to the hash codes to calculate the buckets where these elements would be placed in the hash table.
    • For "apple," the hash code is 3112855979180901583, and it is placed in bucket 3.
    • For "banana," the hash code is 8457036208380033094, and it is placed in bucket 4.
    • For "orange," the hash code is 7578933341900684711, and it is placed in bucket 1.

    Methods for Sets

    Python sets come with various methods to perform operations. Some of the key methods include:

    1. Union Operation on Python Sets (union() or | operator):

    The union of two sets produces a new set containing all unique elements from both sets.

    set1 = [1, 2, 3)
    set2= [3, 4, 5}
    union_set = set1.union (set2)
    # Or: union_set = set1 | set2
    print(union_set)     # Output: (1, 2, 3, 4, 5}

    2. Intersection Operation on Python Sets (intersection() or & operator):

    The Python set intersection of two sets returns a new set with elements shared by both sets.

    set1= {1, 2, 3}
    set2= [3, 4, 5}
    intersection_set = set1. intersection (set2)
    #Or: intersection_set = set1 & set2
    print(intersection_set)   # Output: (3)

    3. Finding Differences of Sets in Python (difference() or - operator):

    The difference between the two sets results in a new set containing elements from the first set but not the second.

    set1 = {1, 2, 3, 4}
    set2 = {3, 4, 5}
    difference_set = set1.difference(set2)
    #Or: difference_set = set1 - set2
    print(difference_set)    # Output: (1, 2)

    Time Complexity of Sets

    Time complexities for common set operations in Python can vary:

    1. Adding an Element (add() method):

    • Average: O(1)
    • Rare hash collision: O(n), where n is the elements in the collided bucket.
    my_set = {1, 2, 3}
    my_set.add(4)    # 0(1) average case

    2. Removing an Element (remove() or discard() methods):

    • Average: O(1)
    • Worst-case with hash collision: O(n), if a collision results in a lengthy linked list.
    my_set = {1, 2, 3}
    my_set.remove(2) # 0(1) average case

    3. Membership Test (in operator):
    Membership checks using the 'in' operator are typically O(1) on average.

    my_set = {1, 2, 3}
    if 2 in my set:   # 0(1) average case 
           print("Element found!")

    4. Union Operation (union() or | operator):
    The union of two sets takes O(len(s)) time, where s is the larger set.

    set1 = {1, 2, 3}
    set2 = {3, 4, 5}
    union_set = set1.union (set2)
    # 0(len(set1))

    5. Intersection Operation (intersection() or & operator):
    The intersection of two sets takes O(min(len(s1), len(s2))) time.

    set1 = (1, 2, 3}
    set2 = {3, 4, 5}
    intersection_set = set1.intersection(set2)     # 0(min(len(set1), len(set2)))

    6. Difference Operation (difference() or - operator):
    The difference between two sets takes O(len(s1)) time.

    set1 = {1, 2, 3, 4}
    set2 = {3, 4, 5}
    difference_set = set1.difference (set2) # 0(len(set1))

    7. Clearing a Set (clear() method):
    Clearing a set using the clear() method is an O(1) operation.

    my_set = (1, 2, 3}
    my_set.clear()    # 0(1)

    Operators for Sets

    Sets provide a range of operators, including union (|), intersection (&), and difference (-), which have been detailed earlier.

    Conclusion

    Python sets are a valuable tool for managing unique data and performing set-based operations. They ensure uniqueness and offer practical solutions for various applications. With this Python set documentation, you can gain the expertise to use sets effectively in your Python applications.

    FAQs

    1. How to create a tuple in Python?
    You can create a tuple by enclosing elements in parentheses. The elements are separated by commas. Example: my_tuple = (1, 2, 3).

    2. What are the common uses of Python frozen sets?

    • Frozen sets serve as dictionary keys
    • For creating collections of sets that must remain unaltered, frozen sets are handy.
    • Due to their hashable and immutable nature, frozen sets are efficient for membership testing in other sets or dictionaries.
    set_of_frozen_sets = {frozenset({1, 2}), frozenset({3, 4})}
    print(frozenset({1, 2}) in set_of_frozen_sets)     # Output: True
    image

    Take our Free Quiz on Python

    Answer quick questions and assess your Python knowledge

    right-top-arrow
    image
    Join 10M+ Learners & Transform Your Career
    Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
    advertise-arrow

    Free Courses

    Explore Our Free Software Tutorials

    upGrad Learner Support

    Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

    text

    Indian Nationals

    1800 210 2020

    text

    Foreign Nationals

    +918045604032

    Disclaimer

    1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

    2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.