Definition<\/strong><\/td>Ordered collection of elements<\/td> Ordered, immutable collection<\/td> Unordered, unique elements<\/td> Unordered collection of key-value pairs<\/td><\/tr> Syntax<\/strong><\/td>[1, 2, 3]<\/code><\/td>(1, 2, 3)<\/code><\/td>{1, 2, 3}<\/code><\/td>{'key1': 'value1', 'key2': 'value2'}<\/code><\/td><\/tr>Mutability<\/strong><\/td>Mutable<\/td> Immutable<\/td> Mutable<\/td> Mutable<\/td><\/tr> Order<\/strong><\/td>Ordered<\/td> Ordered<\/td> Unordered<\/td> Unordered (Python 3.7+ maintains insertion order)<\/td><\/tr> Duplicates<\/strong><\/td>Allows duplicates<\/td> Allows duplicates<\/td> No duplicates<\/td> Keys must be unique; values can be duplicated<\/td><\/tr> Indexing<\/strong><\/td>Supports indexing and slicing<\/td> Supports indexing and slicing<\/td> Does not support indexing<\/td> Keys are used for indexing<\/td><\/tr> Methods<\/strong><\/td>Many methods (e.g., append, remove)<\/td> Limited methods (e.g., count, index)<\/td> Basic methods (e.g., add, remove)<\/td> Rich methods (e.g., get, keys, values, items)<\/td><\/tr> Performance<\/strong><\/td>Slower for lookups<\/td> Faster than lists for fixed data<\/td> Fast membership tests<\/td> Fast lookups by key<\/td><\/tr> Use Cases<\/strong><\/td>Dynamic arrays, frequent updates<\/td> Fixed collections, read-only data<\/td> Membership testing, unique elements<\/td> Mapping, fast access to values by keys<\/td><\/tr> Memory Consumption<\/strong><\/td>More memory for dynamic data<\/td> Less memory, more efficient<\/td> More memory for large sets<\/td> More memory due to keys and values<\/td><\/tr><\/tbody><\/table>comparison list, tuple, sets and dict<\/figcaption><\/figure>Creating Sets<\/h4> You can create a set using curly braces {}<\/code> or the set()<\/code> function. Let’s see both in action<\/p><\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span># Using curly braces<\/span><\/span>\nfruits <\/span>=<\/span> {<\/span>"<\/span>apple<\/span>"<\/span>, <\/span>"<\/span>banana<\/span>"<\/span>, <\/span>"<\/span>cherry<\/span>"<\/span>}<\/span><\/span>\nprint<\/span>(fruits) <\/span>#Output: {'apple', 'banana', 'cherry'}<\/span><\/span>\n<\/span>\n# Using set() function<\/span><\/span>\nvegetables <\/span>=<\/span> <\/span>set<\/span>([<\/span>"<\/span>carrot<\/span>"<\/span>, <\/span>"<\/span>lettuce<\/span>"<\/span>, <\/span>"<\/span>spinach<\/span>"<\/span>])<\/span><\/span>\nprint<\/span>(vegetables) <\/span>#OutPut: {'spinach', 'carrot', 'lettuce'}<\/span><\/span><\/code><\/pre><\/div>Empty Set<\/h4> Creating an empty set is a bit tricky because {}<\/code> creates an empty dictionary. Instead, use set()<\/code>.<\/p><\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span># Correct way to create an empty set<\/span><\/span>\nempty_set <\/span>=<\/span> <\/span>set<\/span>()<\/span><\/span>\nprint<\/span>(empty_set)<\/span><\/span>\n#output: set()<\/span><\/span>\n<\/span><\/code><\/pre><\/div>\n <\/circle>\n <\/circle>\n <\/circle>\n<\/svg><\/div><\/div>Basic Set Operations<\/h1>Method<\/strong><\/th>Description<\/strong><\/th>Behavior if Element Not Present<\/strong><\/th>Return Value<\/strong><\/th>Usage<\/strong><\/th><\/tr><\/thead>remove(x)<\/code><\/td>Removes the specified element x<\/code> from the set.<\/td>Raises a KeyError<\/code>.<\/td>None.<\/td> my_set.remove(x)<\/code><\/td><\/tr>discard(x)<\/code><\/td>Removes the specified element x<\/code> from the set.<\/td>Does nothing (no error).<\/td> None.<\/td> my_set.discard(x)<\/code><\/td><\/tr>pop()<\/code><\/td>Removes and returns an arbitrary element from the set.<\/td> Raises a KeyError<\/code> if the set is empty.<\/td>The removed element.<\/td> my_set.pop()<\/code><\/td><\/tr>clear()<\/code><\/td>Removes all elements from the set, making it empty.<\/td> No effect (no error).<\/td> None.<\/td> my_set.clear()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>Explanation:<\/h3>remove(x)<\/code><\/strong>: Use this when you want to remove a specific element from the set and you expect it to be present. If the element isn’t there, it will raise an error.<\/li>\n\ndiscard(x)<\/code><\/strong>: Similar to remove(x)<\/code>, but it’s safer if you’re unsure whether the element is in the set, as it won’t raise an error if the element is missing.<\/li>\n\npop()<\/code><\/strong>: Handy when you want to remove and retrieve an element from the set, though you can’t control which element will be removed.<\/li>\n\nclear()<\/code><\/strong>: The simplest way to empty a set entirely, remove all elements in one go.<\/li><\/ul>Adding Elements<\/h4> You can add elements to a set using the add()<\/code> method for single items or update()<\/code> for multiple items.<\/p><\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>fruits <\/span>=<\/span> {<\/span>"<\/span>apple<\/span>"<\/span>, <\/span>"<\/span>banana<\/span>"<\/span>, <\/span>"<\/span>cherry<\/span>"<\/span>}<\/span><\/span>\n<\/span>\n# Adding a single element<\/span><\/span>\nfruits.add(<\/span>"<\/span>orange<\/span>"<\/span>)<\/span><\/span>\n