YouTip LogoYouTip

Python3 Func Set

# Python3 set() Function The `set()` function is a built-in Python function used to create a mutable **set** object. In Python, a set is an unordered collection of unique (non-duplicate) elements, matching the mathematical concept of a set. The `set()` function is commonly used to convert other iterable objects into sets, eliminate duplicate values, and perform mathematical set operations like unions and intersections. --- ## Syntax and Parameters ### Syntax ```python set(iterable) ``` ### Parameters * **`iterable`** *(optional)*: * **Type**: Any iterable object (such as a list, tuple, string, dictionary, or range). * **Description**: The sequence or collection to be converted into a set. If omitted, an empty set is returned. ### Return Value * Returns a new, mutable **set** object containing the unique elements from the provided iterable. * **Special Case**: Calling `set()` with no arguments returns an empty set: `set()`. > **Note**: You cannot use empty curly braces `{}` to create an empty set, as `{}` is reserved for creating an empty dictionary (`dict`). --- ## Code Examples ### Example 1: Creating Sets from Different Iterables This example demonstrates how to initialize sets using lists, strings, tuples, and dictionaries, as well as how to create an empty set. ```python # 1. Create a set from a list (automatically removes duplicates) lst = [1, 2, 2, 3, 3, 3, 4] s = set(lst) print(s) # Output: {1, 2, 3, 4} # 2. Create a set from a string (extracts unique characters) s = set("hello") print(s) # Output: {'h', 'e', 'l', 'o'} (order may vary) # 3. Create a set from a tuple t = (1, 2, 3, 2, 1) s = set(t) print(s) # Output: {1, 2, 3} # 4. Create a set from a dictionary (extracts keys only) d = {"a": 1, "b": 2, "c": 3} s = set(d) print(s) # Output: {'a', 'b', 'c'} (order may vary) # 5. Create an empty set s = set() print(s) # Output: set() print(type(s)) # Output: ``` **Expected Output:** ```text {1, 2, 3, 4} {'e', 'h', 'l', 'o'} {1, 2, 3} {'a', 'b', 'c'} set() ``` **Key Takeaways:** 1. **Deduplication**: Duplicate elements in the input iterable are automatically discarded. 2. **Unordered**: Sets do not maintain insertion order. The printed order of elements may vary. 3. **Empty Set**: Always use `set()` to initialize an empty set. --- ### Example 2: Mathematical Set Operations Python sets support standard mathematical operations. You can perform these operations using either operators or built-in set methods. ```python # Define two sets A = {1, 2, 3, 4} B = {3, 4, 5, 6} # 1. Union (elements in A, B, or both) print(A | B) # Output: {1, 2, 3, 4, 5, 6} print(A.union(B)) # Output: {1, 2, 3, 4, 5, 6} # 2. Intersection (elements common to both A and B) print(A & B) # Output: {3, 4} print(A.intersection(B)) # Output: {3, 4} # 3. Difference (elements in A but not in B) print(A - B) # Output: {1, 2} print(A.difference(B)) # Output: {1, 2} # 4. Symmetric Difference (elements in A or B, but not both) print(A ^ B) # Output: {1, 2, 5, 6} print(A.symmetric_difference(B)) # Output: {1, 2, 5, 6} ``` **Expected Output:** ```text {1, 2, 3, 4, 5, 6} {1, 2, 3, 4, 5, 6} {3, 4} {3, 4} {1, 2} {1, 2} {1, 2, 5, 6} {1, 2, 5, 6} ``` --- ## Important Considerations 1. **Hashability**: Elements stored inside a set must be **hashable** (immutable). For example, you can have numbers, strings, or tuples inside a set, but you cannot have mutable objects like lists, dictionaries, or other sets inside a set. 2. **Frozenset**: If you need an immutable version of a set (which can be used as a dictionary key or added to another set), use the `frozenset()` function instead. 3. **Performance**: Checking for membership (`element in my_set`) is highly optimized and runs in $O(1)$ average time complexity, making sets much faster than lists for lookup operations.
← Python3 Func HelpPython3 Func Memoryview β†’