Sets in Python are unordered collections of unique elements that are iterable, mutable, and do not allow duplicates. Sets can be represented using curly braces and provide highly optimized methods for checking if an element is contained. The set() method is used to typecast other data types like lists to sets. Frozen sets are immutable sets created using frozenset() that do not allow modifying elements after creation. Internally, sets use a hash table data structure, where collisions are resolved by linking elements at the same index into a linked list.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
34 views
Sets in Python
Sets in Python are unordered collections of unique elements that are iterable, mutable, and do not allow duplicates. Sets can be represented using curly braces and provide highly optimized methods for checking if an element is contained. The set() method is used to typecast other data types like lists to sets. Frozen sets are immutable sets created using frozenset() that do not allow modifying elements after creation. Internally, sets use a hash table data structure, where collisions are resolved by linking elements at the same index into a linked list.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7
Sets in Python
Done by Pranesh I.B
What is Set? • A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements.
• Set are represented by { } (values enclosed in curly braces)
• The major advantage of using a set, as opposed to a list, is that it has a
highly optimized method for checking whether a specific element is contained in the set. This is based on a data structure known as a hash table. Since sets are unordered, we cannot access items using indexes like we do in lists. Python set() method is used for type casting in Python
• # typecasting list to set
• myset = set(["a", "b", "c"]) • print(myset)
• # Adding element to the set
• myset.add("d") • print(myset) Frozen Set in Python • Python Frozen Sets • Frozen sets in Python are immutable objects that only support methods and operators that produce a result without affecting the frozen set or sets to which they are applied. It can be done with frozenset() method in Python.
• While elements of a set can be modified at any time, elements of the
frozen set remain the same after creation.
• If no parameters are passed, it returns an empty frozenset.
Working of Set • Internal working of Set • This is based on a data structure known as a hash table. • If Multiple values are present at the same index position, then the value is appended to that index position, to form a Linked List. In, Python Sets are implemented using dictionary with dummy variables, where key beings the members set with greater optimizations to the time complexity.