{"id":453,"date":"2025-05-20T04:46:19","date_gmt":"2025-05-20T04:46:19","guid":{"rendered":"https:\/\/buhave.com\/courses\/?p=453"},"modified":"2025-05-20T12:56:49","modified_gmt":"2025-05-20T12:56:49","slug":"data-structures","status":"publish","type":"post","link":"https:\/\/buhave.com\/courses\/python\/data-structures\/","title":{"rendered":"Data Structures"},"content":{"rendered":"<h2>Lists and list methods<span style=\"font-weight: 400\"><br \/>\n<\/span><\/h2>\n<h3>What Are Lists in Python?<\/h3>\n<p>A list is a built-in data structure in Python that is used to store multiple items in a single variable. Lists are ordered, mutable, and can contain elements of mixed data types (strings, numbers, booleans, other lists, etc.).<\/p>\n<p><strong>List Syntax<\/strong><\/p>\n<p style=\"text-align: center\"><em>my_list = [1, 2, 3, &#8220;hello&#8221;, True]<\/em><\/p>\n<ul>\n<li>Items are separated by commas.<\/li>\n<li>Enclosed in square brackets [].<\/li>\n<li>Indexed starting from 0.<\/li>\n<\/ul>\n<p><strong>Accessing List Items<\/strong><\/p>\n<p style=\"text-align: center\"><em>fruits = [&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;]<\/em><br \/>\n<em>print(fruits[0]) # Output: apple<\/em><br \/>\n<em>print(fruits[-1]) # Output: cherry (negative index = from end)<\/em><\/p>\n<p><strong>Common List Methods<\/strong><\/p>\n<p>Python provides many built-in methods to manipulate lists:<\/p>\n<p><strong>append(item)<\/strong><\/p>\n<p>Adds a new item to the end of the list.<\/p>\n<p style=\"text-align: center\"><em>fruits.append(&#8220;orange&#8221;)<\/em><\/p>\n<p><strong>insert(index, item)<\/strong><\/p>\n<p>Inserts an item at a specific position.<\/p>\n<p style=\"text-align: center\"><em>fruits.insert(1, &#8220;kiwi&#8221;) # Inserts &#8220;kiwi&#8221; at index 1<\/em><\/p>\n<p><strong>extend(iterable)<\/strong><\/p>\n<p>Extends the list by appending elements from another iterable (like another list).<\/p>\n<p style=\"text-align: center\"><em>fruits.extend([&#8220;mango&#8221;, &#8220;grape&#8221;])<\/em><\/p>\n<p><strong>remove(item)<\/strong><\/p>\n<p>Removes the first occurrence of the specified value.<\/p>\n<p style=\"text-align: center\"><em>fruits.remove(&#8220;banana&#8221;)<\/em><\/p>\n<p><strong>pop([index])<\/strong><\/p>\n<p>Removes and returns the item at the given index (or last item if no index is given).<\/p>\n<p style=\"text-align: center\"><em>last_fruit = fruits.pop() # Removes last item<\/em><\/p>\n<p><strong>clear()<\/strong><\/p>\n<p>Removes all items from the list.<\/p>\n<p style=\"text-align: center\"><em>fruits.clear()<\/em><\/p>\n<p><strong>index(item)<\/strong><\/p>\n<p>Returns the index of the first occurrence of the specified item.<\/p>\n<p style=\"text-align: center\"><em>idx = fruits.index(&#8220;apple&#8221;)<\/em><\/p>\n<p><strong>count(item)<\/strong><\/p>\n<p>Returns the number of times a value appears in the list.<\/p>\n<p style=\"text-align: center\"><em>nums = [1, 2, 2, 3, 2]<\/em><br \/>\n<em>print(nums.count(2)) # Output: 3<\/em><\/p>\n<p><strong>sort()<\/strong><\/p>\n<p>Sorts the list in-place (modifies the original list). For ascending order by default.<\/p>\n<p style=\"text-align: center\"><em>numbers = [4, 2, 9, 1]<\/em><br \/>\n<em>numbers.sort()<\/em><\/p>\n<p>Use numbers.sort(reverse=True) for descending.<\/p>\n<p><strong>sorted(list)<\/strong><\/p>\n<p>Returns a new sorted list without modifying the original.<\/p>\n<p style=\"text-align: center\"><em>sorted_numbers = sorted(numbers)<\/em><\/p>\n<p><strong>reverse()<\/strong><\/p>\n<p>Reverses the elements of the list in-place.<\/p>\n<p style=\"text-align: center\"><em>numbers.reverse()<\/em><\/p>\n<p><strong>copy()<\/strong><\/p>\n<p>Creates a shallow copy of the list.<\/p>\n<p style=\"text-align: center\"><em>new_list = fruits.copy()<\/em><\/p>\n<p><strong>Looping Through Lists:<\/strong><\/p>\n<p style=\"text-align: center\"><em>for fruit in fruits:<\/em><br \/>\n<em>print(fruit)<\/em><\/p>\n<p><strong>List Slicing<\/strong><\/p>\n<p style=\"text-align: center\"><em>nums = [0, 1, 2, 3, 4, 5]<\/em><br \/>\n<em>print(nums[1:4]) # Output: [1, 2, 3]<\/em><br \/>\n<em>print(nums[:3]) # Output: [0, 1, 2]<\/em><br \/>\n<em>print(nums[::2]) # Output: [0, 2, 4]<\/em><\/p>\n<p><strong>Lists Are Mutable<\/strong><\/p>\n<p>You can modify individual items:<\/p>\n<p style=\"text-align: center\"><em>nums[0] = 100<\/em><\/p>\n<p>And delete with del:<\/p>\n<p style=\"text-align: center\"><em>del nums[2]<\/em><\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Lists are ordered, mutable, and can hold mixed data types.<\/li>\n<li>Access items using indexes.<\/li>\n<li>Modify lists with built-in methods like append(), remove(), sort(), and more.<\/li>\n<li>Use slicing, loops, and comprehensions for powerful list operations.<\/li>\n<\/ul>\n<h2>Tuples and tuple unpacking<\/h2>\n<h3>What Are Tuples?<\/h3>\n<p>A tuple is a built-in data type in Python used to store multiple items in a single, ordered, immutable collection.<\/p>\n<ul>\n<li>Like a list, but cannot be changed (immutable) after creation.<\/li>\n<li>Often used for fixed data (e.g., coordinates, RGB colors, function returns).<\/li>\n<\/ul>\n<p><strong>Tuple Syntax<\/strong><\/p>\n<p style=\"text-align: center\"><em>my_tuple = (1, 2, 3)<\/em><\/p>\n<ul>\n<li>Defined with parentheses () \u2014 although parentheses are optional.<\/li>\n<li>Elements separated by commas.<\/li>\n<li>Can hold mixed data types.<\/li>\n<\/ul>\n<p><strong>One-item tuple (very important!)<\/strong><\/p>\n<p>To define a tuple with one item, you must include a comma:<\/p>\n<p style=\"text-align: center\"><em>single = (5,) # This is a tuple<\/em><br \/>\n<em>not_a_tuple = (5) # This is just an int<\/em><\/p>\n<p><strong>Accessing Tuple Elements<\/strong><\/p>\n<p>Tuples are indexed, just like lists:<\/p>\n<p style=\"text-align: center\"><em>colors = (&#8220;red&#8221;, &#8220;green&#8221;, &#8220;blue&#8221;)<\/em><br \/>\n<em>print(colors[0]) # red<\/em><br \/>\n<em>print(colors[-1]) # blue<\/em><\/p>\n<p><strong>Looping Through Tuples<\/strong><\/p>\n<p style=\"text-align: center\"><em>for color in colors:<\/em><br \/>\n<em>print(color)<\/em><\/p>\n<p><strong>Tuples Are Immutable<\/strong><\/p>\n<p>You cannot change, add, or remove items from a tuple:<\/p>\n<p style=\"text-align: center\"><em>colors[0] = &#8220;yellow&#8221; #\u00a0 TypeError<\/em><\/p>\n<h3>Tuple Methods<\/h3>\n<ul>\n<li>Tuples have fewer methods than lists due to immutability:<\/li>\n<li>count(value) \u2013 returns the number of times a value appears.<\/li>\n<li>index(value) \u2013 returns the index of the first matching value.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>nums = (1, 2, 2, 3)<\/em><br \/>\n<em>print(nums.count(2)) # 2<\/em><br \/>\n<em>print(nums.index(3)) # 3<\/em><\/p>\n<h3>Tuple Unpacking<\/h3>\n<p>Tuple unpacking lets you assign values from a tuple to multiple variables in one line.<\/p>\n<p><strong>Basic Unpacking<\/strong><\/p>\n<p style=\"text-align: center\"><em>point = (10, 20)<\/em><br \/>\n<em>x, y = point<\/em><br \/>\n<em>print(x) # 10 <\/em><em>print(y) # 20<\/em><\/p>\n<p>The number of variables must match the number of tuple items.<\/p>\n<p><strong>Swapping Variables (Pythonic Way)<\/strong><\/p>\n<p style=\"text-align: center\"><em>a = 5<\/em><br \/>\n<em>b = 10<\/em><br \/>\n<em>a, b = b, a<\/em><br \/>\n<em>print(a, b) # Output: 10 5<\/em><\/p>\n<p>No need for a temporary variable!<\/p>\n<p><strong>Unpacking with * (Extended Unpacking)<\/strong><\/p>\n<p style=\"text-align: center\"><em>a, *b = (1, 2, 3, 4)<\/em><br \/>\n<em>print(a) # 1<\/em><br \/>\n<em>print(b) # [2, 3, 4]<\/em><\/p>\n<p style=\"text-align: center\"><em>a, *b, c = (1, 2, 3, 4, 5)<\/em><br \/>\n<em>print(a) # 1<\/em><br \/>\n<em>print(b) # [2, 3, 4]<\/em><br \/>\n<em>print(c) # 5<\/em><\/p>\n<p><strong>Tuple Unpacking in Functions<\/strong><\/p>\n<p>You can return multiple values from a function using a tuple:<\/p>\n<p style=\"text-align: center\"><em>def get_user():<\/em><br \/>\n<em>return &#8220;Alice&#8221;, 30 # returns a tuple<\/em><\/p>\n<p style=\"text-align: center\"><em>name, age = get_user()<\/em><br \/>\n<em>print(name) # Alice<\/em><br \/>\n<em>print(age) # 30<\/em><\/p>\n<p><strong>Summary:<\/strong><\/p>\n<ul>\n<li>Tuples are ordered, immutable sequences.<\/li>\n<li>Useful for fixed data or function returns.<\/li>\n<li>Use tuple unpacking to assign values in a clean way.<\/li>\n<li>Tuples support indexing, looping, and limited methods (count(), index()).<\/li>\n<\/ul>\n<h2>Dictionaries and dictionary methods<\/h2>\n<h3>What Is a Dictionary?<\/h3>\n<p>A dictionary is a collection of key-value pairs, where each key is unique, and each key maps to a specific value. Think of it like a real dictionary: each word (key) has a definition (value).<\/p>\n<p><strong>Dictionary Syntax<\/strong><\/p>\n<p style=\"text-align: center\"><em>my_dict = {<\/em><br \/>\n<em>&#8220;name&#8221;: &#8220;Alice&#8221;,<\/em><br \/>\n<em>&#8220;age&#8221;: 30,<\/em><br \/>\n<em>&#8220;is_active&#8221;: True<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>Keys and values are separated by colons.<\/li>\n<li>Pairs are separated by commas.<\/li>\n<li>Enclosed in curly braces {}.<\/li>\n<\/ul>\n<p><strong>Accessing Values<\/strong><\/p>\n<p style=\"text-align: center\"><em>print(my_dict[&#8220;name&#8221;]) # Output: Alice<\/em><\/p>\n<p>Use the key to access the value.<\/p>\n<p><strong>Using get() Method<\/strong><\/p>\n<p style=\"text-align: center\"><em>print(my_dict.get(&#8220;name&#8221;)) # Alice<\/em><br \/>\n<em>print(my_dict.get(&#8220;email&#8221;, &#8220;N\/A&#8221;)) # N\/A if key not found<\/em><\/p>\n<p>Safer than bracket access because it avoids KeyError.<\/p>\n<p><strong>Adding &amp; Updating Items<\/strong><\/p>\n<p style=\"text-align: center\"><em>my_dict[&#8220;email&#8221;] = &#8220;alice@example.com&#8221; # Add new<\/em><br \/>\n<em>my_dict[&#8220;age&#8221;] = 31 # Update existing<\/em><\/p>\n<p><strong>Removing Items<\/strong><\/p>\n<p style=\"text-align: center\"><em>my_dict.pop(&#8220;age&#8221;) # Removes and returns the value for &#8216;age&#8217;<\/em><br \/>\n<em>del my_dict[&#8220;is_active&#8221;] # Removes key &#8216;is_active&#8217;<\/em><\/p>\n<p><strong>Clearing the Dictionary<\/strong><\/p>\n<p style=\"text-align: center\"><em>my_dict.clear() # Removes all items<\/em><\/p>\n<p><strong>Dictionary Methods<\/strong><\/p>\n<p>Here are some of the most commonly used dictionary methods:<\/p>\n<table style=\"width: 100%;border-collapse: collapse;font-family: Arial, sans-serif;margin: 20px 0\">\n<thead>\n<tr>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>Method<\/strong><\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>Description<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>get(key, default)<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Returns value for key, or default if key not found<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>keys()<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Returns a view object of all keys<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>values()<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Returns a view object of all values<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>items()<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Returns a view object of (key, value) pairs<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>update(dict2)<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Merges dictionary with another dictionary&#8217;s key-value pairs<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>pop(key)<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Removes and returns the item with the specified key<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>popitem()<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Removes and returns the last inserted (key, value) pair<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>clear()<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Removes all items from the dictionary<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example:<\/strong> Looping Through a Dictionary<\/p>\n<p style=\"text-align: center\"><em>for key in my_dict:<\/em><br \/>\n<em>print(key, my_dict[key])<\/em><\/p>\n<p><strong>Or with .items()<\/strong><\/p>\n<p style=\"text-align: center\"><em>for key, value in my_dict.items():<\/em><br \/>\n<em>print(f&#8221;{key}: {value}&#8221;)<\/em><\/p>\n<p><strong>Check If a Key Exists<\/strong><\/p>\n<p style=\"text-align: center\"><em>if &#8220;name&#8221; in my_dict:<\/em><br \/>\n<em>print(&#8220;Found name&#8221;)<\/em><\/p>\n<p><strong>Dictionary Comprehensions<\/strong><\/p>\n<p style=\"text-align: center\"><em>squares = {x: x**2 for x in range(5)}<\/em><br \/>\n<em>print(squares) # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}<\/em><\/p>\n<p><strong>Nesting Dictionaries<\/strong><\/p>\n<p style=\"text-align: center\"><em>users = {<\/em><br \/>\n<em>&#8220;alice&#8221;: {&#8220;age&#8221;: 25, &#8220;email&#8221;: &#8220;a@example.com&#8221;},<\/em><br \/>\n<em>&#8220;bob&#8221;: {&#8220;age&#8221;: 30, &#8220;email&#8221;: &#8220;b@example.com&#8221;}<\/em><br \/>\n<em>}<\/em><br \/>\n<em>print(users[&#8220;bob&#8221;][&#8220;email&#8221;]) # b@example.com<\/em><\/p>\n<p><strong>Summary:<\/strong><\/p>\n<ul>\n<li>Dictionaries store key-value pairs and are unordered (but maintain insertion order in Python 3.7+).<\/li>\n<li>Keys must be immutable and unique (e.g., strings, numbers, tuples).<\/li>\n<li>Use methods like .get(), .items(), .update(), and .pop() to work with them efficiently.<\/li>\n<li>Great for representing structured data, such as JSON, configurations, and more.<\/li>\n<\/ul>\n<h2>Sets and set operations<\/h2>\n<h3>What Is a Set in Python?<\/h3>\n<p>A set is an unordered, mutable, and unindexed collection of unique elements. Sets are great when you need to eliminate duplicates or perform set-based logic.<\/p>\n<p><strong>Set Syntax<\/strong><\/p>\n<p style=\"text-align: center\"><em>my_set = {1, 2, 3, 4}<\/em><\/p>\n<ul>\n<li>Use curly braces {} or the set() constructor.<\/li>\n<li>Duplicate values are automatically removed.<\/li>\n<\/ul>\n<p>Creating an empty set must use set() \u2014 {} creates an empty dictionary, not a set.<\/p>\n<p style=\"text-align: center\"><em>empty_set = set()<\/em><\/p>\n<p><strong>Example:<\/strong> Duplicates Removed Automatically<\/p>\n<p style=\"text-align: center\"><em>nums = {1, 2, 2, 3, 3}<\/em><br \/>\n<em>print(nums) # Output: {1, 2, 3}<\/em><\/p>\n<p><strong>Common Set Methods<\/strong><\/p>\n<table style=\"width: 100%;border-collapse: collapse;font-family: Arial, sans-serif;margin: 20px 0\">\n<thead>\n<tr>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\">Method<\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>add(item)<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Adds a single item to the set<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>update(iterable)<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Adds multiple items from an iterable<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>remove(item)<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Removes the specified item (raises KeyError if item doesn&#8217;t exist)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>discard(item)<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Removes the specified item (does nothing if item doesn&#8217;t exist)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>pop()<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Removes and returns an arbitrary item from the set (raises KeyError if empty)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>clear()<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Removes all items from the set<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>copy()<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Returns a new set with a shallow copy of the elements<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example Methods:<\/strong><\/p>\n<p style=\"text-align: center\"><em>s = {1, 2, 3}<\/em><br \/>\n<em>s.add(4) # {1, 2, 3, 4}<\/em><br \/>\n<em>s.update([5, 6]) # {1, 2, 3, 4, 5, 6}<\/em><br \/>\n<em>s.remove(3) # {1, 2, 4, 5, 6}<\/em><br \/>\n<em>s.discard(10) # No error if 10 isn&#8217;t there<\/em><\/p>\n<p><strong>Looping Through a Set<\/strong><\/p>\n<p style=\"text-align: center\"><em>for item in s:<\/em><br \/>\n<em>print(item)<\/em><\/p>\n<p>Order is not guaranteed in sets.<\/p>\n<p><strong>Set Operations (like in Math)<\/strong><\/p>\n<p><strong>union() or |<\/strong><\/p>\n<p>Combine all unique elements from both sets.<\/p>\n<p style=\"text-align: center\"><em>a = {1, 2, 3}<\/em><br \/>\n<em>b = {3, 4, 5}<\/em><br \/>\n<em>print(a | b) # {1, 2, 3, 4, 5}<\/em><br \/>\n<em>print(a.union(b)) # Same<\/em><\/p>\n<p><strong>intersection() or &amp;<\/strong><\/p>\n<p>Get only the elements common to both sets.<\/p>\n<p style=\"text-align: center\"><em>print(a &amp; b) # {3}<\/em><br \/>\n<em>print(a.intersection(b)) # Same<\/em><\/p>\n<p><strong>difference() or &#8211;<\/strong><\/p>\n<p>Get elements that are in the first set but not in the second.<\/p>\n<p style=\"text-align: center\"><em>print(a &#8211; b) # {1, 2}<\/em><br \/>\n<em>print(a.difference(b)) # Same<\/em><\/p>\n<p><strong>symmetric_difference() or ^<\/strong><\/p>\n<p>Get elements in either set, but not both.<\/p>\n<p style=\"text-align: center\"><em>print(a ^ b) # {1, 2, 4, 5}<\/em><br \/>\n<em>print(a.symmetric_difference(b)) # Same<\/em><\/p>\n<p><strong>Set Comparisons<\/strong><\/p>\n<table style=\"width: 100%;border-collapse: collapse;font-family: Arial, sans-serif;margin: 20px 0\">\n<thead>\n<tr>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>Operation<\/strong><\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>Meaning<\/strong><\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>Example<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>==<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Sets contain exactly the same elements<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>{1, 2} == {2, 1}<\/code> \u2192 <code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>issubset()<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">All elements of set A exist in set B<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>{1, 2}.issubset({1, 2, 3})<\/code> \u2192 <code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>issuperset()<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">All elements of set B exist in set A<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>{1, 2, 3}.issuperset({1, 2})<\/code> \u2192 <code>True<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>isdisjoint()<\/code><\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Sets share no common elements<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\"><code>{1, 2}.isdisjoint({3, 4})<\/code> \u2192 <code>True<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>a = {1, 2}<\/em><br \/>\n<em>b = {1, 2, 3}<\/em><\/p>\n<p style=\"text-align: center\"><em>print(a.issubset(b)) # True<\/em><br \/>\n<em>print(b.issuperset(a)) # True<\/em><br \/>\n<em>print(a.isdisjoint({4})) # True<\/em><\/p>\n<p><strong>Summary:<\/strong><\/p>\n<ul>\n<li>Sets store unique, unordered values.<\/li>\n<li>Use sets to remove duplicates and perform mathematical operations.<\/li>\n<li>Built-in methods like union(), intersection(), and difference() make sets powerful for logic and data comparisons.<\/li>\n<\/ul>\n<p>Want to learn about frozensets (immutable sets), set comprehensions, or use cases like duplicates removal in lists? Just let me know!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lists and list methods What Are Lists in Python? A list is a built-in data structure in Python that is used to store multiple items in a single variable. Lists are ordered, mutable, and can contain elements of mixed data types (strings, numbers, booleans, other<\/p>\n","protected":false},"author":1,"featured_media":454,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Data Structures - Python Course<\/title>\n<meta name=\"description\" content=\"Data structures help organize and manage collections of values efficiently for processing, retrieval, and manipulation in programs.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/buhave.com\/courses\/python\/data-structures\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Data Structures - Python Course\" \/>\n<meta property=\"og:description\" content=\"Data structures help organize and manage collections of values efficiently for processing, retrieval, and manipulation in programs.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/buhave.com\/courses\/python\/data-structures\/\" \/>\n<meta property=\"og:site_name\" content=\"BUHAVE\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BeYouHave\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/naveedsafdarawan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-20T04:46:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-20T12:56:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Data-Structures.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Naveed Safdar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Naveed Safdar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/\"},\"author\":{\"name\":\"Naveed Safdar\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\"},\"headline\":\"Data Structures\",\"datePublished\":\"2025-05-20T04:46:19+00:00\",\"dateModified\":\"2025-05-20T12:56:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/\"},\"wordCount\":1419,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Data-Structures.webp\",\"articleSection\":[\"Python Course\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/\",\"name\":\"Data Structures - Python Course\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Data-Structures.webp\",\"datePublished\":\"2025-05-20T04:46:19+00:00\",\"dateModified\":\"2025-05-20T12:56:49+00:00\",\"description\":\"Data structures help organize and manage collections of values efficiently for processing, retrieval, and manipulation in programs.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/#primaryimage\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Data-Structures.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Data-Structures.webp\",\"width\":1200,\"height\":628,\"caption\":\"Data Structures\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/data-structures\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Course\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/learn\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Data Structures\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"name\":\"BUHAVE\",\"description\":\"Courses - Learn Online for Free\",\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/buhave.com\\\/courses\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\",\"name\":\"BUHAVE\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"width\":375,\"height\":75,\"caption\":\"BUHAVE\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/BeYouHave\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/buhave\",\"https:\\\/\\\/www.youtube.com\\\/@buhave\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\",\"name\":\"Naveed Safdar\",\"description\":\"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/naveedsafdar\\\/\",\"https:\\\/\\\/www.facebook.com\\\/naveedsafdarawan\\\/\",\"https:\\\/\\\/www.youtube.com\\\/@naveedsafdar\"],\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/author\\\/naveed-safdar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Data Structures - Python Course","description":"Data structures help organize and manage collections of values efficiently for processing, retrieval, and manipulation in programs.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/buhave.com\/courses\/python\/data-structures\/","og_locale":"en_US","og_type":"article","og_title":"Data Structures - Python Course","og_description":"Data structures help organize and manage collections of values efficiently for processing, retrieval, and manipulation in programs.","og_url":"https:\/\/buhave.com\/courses\/python\/data-structures\/","og_site_name":"BUHAVE","article_publisher":"https:\/\/www.facebook.com\/BeYouHave\/","article_author":"https:\/\/www.facebook.com\/naveedsafdarawan\/","article_published_time":"2025-05-20T04:46:19+00:00","article_modified_time":"2025-05-20T12:56:49+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Data-Structures.webp","type":"image\/webp"}],"author":"Naveed Safdar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Safdar","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/buhave.com\/courses\/python\/data-structures\/#article","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/python\/data-structures\/"},"author":{"name":"Naveed Safdar","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca"},"headline":"Data Structures","datePublished":"2025-05-20T04:46:19+00:00","dateModified":"2025-05-20T12:56:49+00:00","mainEntityOfPage":{"@id":"https:\/\/buhave.com\/courses\/python\/data-structures\/"},"wordCount":1419,"commentCount":0,"publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"image":{"@id":"https:\/\/buhave.com\/courses\/python\/data-structures\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Data-Structures.webp","articleSection":["Python Course"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/buhave.com\/courses\/python\/data-structures\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/buhave.com\/courses\/python\/data-structures\/","url":"https:\/\/buhave.com\/courses\/python\/data-structures\/","name":"Data Structures - Python Course","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/#website"},"primaryImageOfPage":{"@id":"https:\/\/buhave.com\/courses\/python\/data-structures\/#primaryimage"},"image":{"@id":"https:\/\/buhave.com\/courses\/python\/data-structures\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Data-Structures.webp","datePublished":"2025-05-20T04:46:19+00:00","dateModified":"2025-05-20T12:56:49+00:00","description":"Data structures help organize and manage collections of values efficiently for processing, retrieval, and manipulation in programs.","breadcrumb":{"@id":"https:\/\/buhave.com\/courses\/python\/data-structures\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/buhave.com\/courses\/python\/data-structures\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/python\/data-structures\/#primaryimage","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Data-Structures.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Data-Structures.webp","width":1200,"height":628,"caption":"Data Structures"},{"@type":"BreadcrumbList","@id":"https:\/\/buhave.com\/courses\/python\/data-structures\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Courses","item":"https:\/\/buhave.com\/courses\/"},{"@type":"ListItem","position":2,"name":"Python Course","item":"https:\/\/buhave.com\/courses\/learn\/python\/"},{"@type":"ListItem","position":3,"name":"Data Structures"}]},{"@type":"WebSite","@id":"https:\/\/buhave.com\/courses\/#website","url":"https:\/\/buhave.com\/courses\/","name":"BUHAVE","description":"Courses - Learn Online for Free","publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/buhave.com\/courses\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/buhave.com\/courses\/#organization","name":"BUHAVE","url":"https:\/\/buhave.com\/courses\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","width":375,"height":75,"caption":"BUHAVE"},"image":{"@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/BeYouHave\/","https:\/\/www.linkedin.com\/company\/buhave","https:\/\/www.youtube.com\/@buhave"]},{"@type":"Person","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca","name":"Naveed Safdar","description":"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.","sameAs":["https:\/\/www.linkedin.com\/in\/naveedsafdar\/","https:\/\/www.facebook.com\/naveedsafdarawan\/","https:\/\/www.youtube.com\/@naveedsafdar"],"url":"https:\/\/buhave.com\/courses\/author\/naveed-safdar\/"}]}},"_links":{"self":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/comments?post=453"}],"version-history":[{"count":1,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/453\/revisions"}],"predecessor-version":[{"id":455,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/453\/revisions\/455"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media\/454"}],"wp:attachment":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media?parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/categories?post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/tags?post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}