{"id":32781,"date":"2022-08-05T16:07:32","date_gmt":"2022-08-05T16:07:32","guid":{"rendered":"https:\/\/www.askpython.com\/?p=32781"},"modified":"2022-08-05T16:07:34","modified_gmt":"2022-08-05T16:07:34","slug":"data-structures-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/data-structures-in-python","title":{"rendered":"Data Structures in Python"},"content":{"rendered":"\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/08\/python-data-structures-1024x683.png\" alt=\"Python Data Structures\" class=\"wp-image-32909\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/08\/python-data-structures-1024x683.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/08\/python-data-structures-300x200.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/08\/python-data-structures-768x512.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/08\/python-data-structures.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In any programming language, we need to deal with data.&nbsp; Now, one of the most fundamental things that we need to work with the data is to <strong>store, manage, and access it efficiently<\/strong> in an organized way so it can be utilized whenever required for our purposes. Data Structures are used to take care of all our needs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Data Structures?<\/h2>\n\n\n\n<p>Data Structures are fundamental building blocks of a programming language. It aims to provide a systematic approach to fulfill all the requirements mentioned previously in the article. The data structures in Python are <strong>List, Tuple, Dictionary, and Set<\/strong>. They are regarded as <strong>implicit or built-in Data Structures in Python<\/strong>. We can use these data structures and apply numerous methods to them to manage, relate, manipulate and utilize our data.<\/p>\n\n\n\n<p>We also have custom Data Structures that are <strong>user-defined<\/strong> namely <strong><a href=\"https:\/\/www.askpython.com\/python\/python-stack\" data-type=\"post\" data-id=\"1517\">Stack<\/a>, <a href=\"https:\/\/www.askpython.com\/python-modules\/python-queue\" data-type=\"post\" data-id=\"3540\">Queue<\/a>, <a href=\"https:\/\/www.askpython.com\/python\/examples\/binary-tree-implementation\" data-type=\"post\" data-id=\"12397\">Tree<\/a>, <a href=\"https:\/\/www.askpython.com\/python\/examples\/linked-lists-in-python\" data-type=\"post\" data-id=\"9366\">Linked List<\/a>, and <a href=\"https:\/\/www.askpython.com\/python\/examples\/graph-in-python\" data-type=\"post\" data-id=\"17624\">Graph<\/a><\/strong>. They allow users to have full control over their functionality and use them for advanced programming purposes. However, <em>we will be focussing on the built-in Data Structures for this article.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"861\" height=\"507\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/08\/Implicit-Data-Structures-Python.jpg\" alt=\"Implicit Data Structures Python\" class=\"wp-image-32782\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/08\/Implicit-Data-Structures-Python.jpg 861w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/08\/Implicit-Data-Structures-Python-300x177.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/08\/Implicit-Data-Structures-Python-768x452.jpg 768w\" sizes=\"auto, (max-width: 861px) 100vw, 861px\" \/><figcaption>Implicit Data Structures Python<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">LIST<\/h2>\n\n\n\n<p>Lists help us to store our data sequentially with multiple data types. They are comparable to arrays with the exception that they can store different data types like strings and numbers at the same time. Every item or element in a list has an assigned index. Since <em>Python uses 0-based indexing<\/em>, the first element has an index of 0 and the counting goes on. The last element of a list starts with -1 which can be used to access the elements from the last to the first. To create a list we have to write the items inside the <strong>square brackets<\/strong>.<\/p>\n\n\n\n<p><em>One of the most important things to remember about lists<\/em> is that they are <strong>Mutable<\/strong>. This simply means that we can change an element in a list by accessing it directly as part of the assignment statement using the indexing operator.&nbsp; We can also perform operations on our list to get desired output. Let\u2019s go through the code to gain a better understanding of list and list operations.<\/p>\n\n\n\n<p><strong>1. Creating a List<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#creating the list\nmy_list = &#x5B;&#039;p&#039;, &#039;r&#039;, &#039;o&#039;, &#039;b&#039;, &#039;e&#039;]\nprint(my_list)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;&#039;p&#039;, &#039;r&#039;, &#039;o&#039;, &#039;b&#039;, &#039;e&#039;]\n<\/pre><\/div>\n\n\n<p><strong>2. Accessing items from the List<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#accessing the list \n\n#accessing the first item of the list\nmy_list&#x5B;0]\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#039;p&#039;\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#accessing the third item of the list\nmy_list&#x5B;2]\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#039;o&#039;\n<\/pre><\/div>\n\n\n<p><strong>3. Adding new items to the list<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#adding item to the list\nmy_list + &#x5B;&#039;k&#039;]\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#039;p&#039;, &#039;r&#039;, &#039;o&#039;, &#039;b&#039;, &#039;e&#039;, &#039;k&#039;]\n<\/pre><\/div>\n\n\n<p><strong>4. Removing Items<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#removing item from the list\n#Method 1:\n\n#Deleting list items\nmy_list = &#x5B;&#039;p&#039;, &#039;r&#039;, &#039;o&#039;, &#039;b&#039;, &#039;l&#039;, &#039;e&#039;, &#039;m&#039;]\n\n# delete one item\ndel my_list&#x5B;2]\n\nprint(my_list)\n\n# delete multiple items\ndel my_list&#x5B;1:5]\n\nprint(my_list)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;&#039;p&#039;, &#039;r&#039;, &#039;b&#039;, &#039;l&#039;, &#039;e&#039;, &#039;m&#039;]\n&#x5B;&#039;p&#039;, &#039;m&#039;]\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#Method 2:\n\n#with remove fucntion\nmy_list = &#x5B;&#039;p&#039;,&#039;r&#039;,&#039;o&#039;,&#039;k&#039;,&#039;l&#039;,&#039;y&#039;,&#039;m&#039;]\nmy_list.remove(&#039;p&#039;)\n\n\nprint(my_list)\n\n#Method 3:\n\n#with pop function\nprint(my_list.pop(1))\n\n# Output: &#x5B;&#039;r&#039;, &#039;k&#039;, &#039;l&#039;, &#039;y&#039;, &#039;m&#039;]\nprint(my_list)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;&#039;r&#039;, &#039;o&#039;, &#039;k&#039;, &#039;l&#039;, &#039;y&#039;, &#039;m&#039;]\no\n&#x5B;&#039;r&#039;, &#039;k&#039;, &#039;l&#039;, &#039;y&#039;, &#039;m&#039;]\n<\/pre><\/div>\n\n\n<p><strong>5. Sorting List<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#sorting of list in ascending order\n\nmy_list.sort()\nprint(my_list)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;&#039;k&#039;, &#039;l&#039;, &#039;m&#039;, &#039;r&#039;, &#039;y&#039;]\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#sorting of list in descending order\n\nmy_list.sort(reverse=True)\nprint(my_list)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;&#039;y&#039;, &#039;r&#039;, &#039;m&#039;, &#039;l&#039;, &#039;k&#039;]\n<\/pre><\/div>\n\n\n<p><strong>6. Finding the length of a List<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#finding the length of list\n\nlen(my_list)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n5\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">TUPLE<\/h2>\n\n\n\n<p>Tuples are very similar to lists with a key difference that <strong>a tuple is IMMUTABLE<\/strong>, unlike a list. Once we create a tuple or have a tuple, we are not allowed to change the elements inside it. However, if we have an element inside a tuple, which is a list itself, only then we can access or change within that list. To create a tuple, we have to write the items inside the <strong>parenthesis<\/strong>. Like the lists, we have similar methods which can be used with tuples. Let\u2019s go through some code snippets to understand using tuples.<\/p>\n\n\n\n<p><strong>1. Creating a Tuple<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#creating of tuple\n\nmy_tuple = (&quot;apple&quot;, &quot;banana&quot;, &quot;guava&quot;)\nprint(my_tuple)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n(&#039;apple&#039;, &#039;banana&#039;, &#039;guava&#039;)\n<\/pre><\/div>\n\n\n<p><strong>2. Accessing items from a Tuple<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#accessing first element in tuple\n\nmy_tuple&#x5B;1]\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#039;banana&#039;\n<\/pre><\/div>\n\n\n<p><strong>3. Length of a Tuple<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#for finding the lenght of tuple\n\nlen(my_tuple)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n3\n<\/pre><\/div>\n\n\n<p><strong>4. Converting a Tuple to List<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#converting tuple into a list\n\nmy_tuple_list = list(my_tuple)\ntype(my_tuple_list)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlist\n<\/pre><\/div>\n\n\n<p><strong>5. Reversing a Tuple<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#Reversing a tuple\n\ntuple(sorted(my_tuple, reverse=True)) \n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n(&#039;guava&#039;, &#039;banana&#039;, &#039;apple&#039;)\n<\/pre><\/div>\n\n\n<p><strong>6. Sorting a Tuple<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#sorting tuple in ascending order\n\ntuple(sorted(my_tuple)) \n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n(&#039;apple&#039;, &#039;banana&#039;, &#039;guava&#039;)\n<\/pre><\/div>\n\n\n<p><strong>7. Removing elements from Tuple <\/strong><\/p>\n\n\n\n<p>For removing elements from the tuple, we first converted the tuple into a list as we did in one of our methods above( Point No. 4) then followed the same process of the list, and explicitly removed an entire tuple, just\u00a0using<strong> the del statement<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">DICTIONARY<\/h2>\n\n\n\n<p>Dictionary is a collection which simply means that it is used to store a value with some key and extract the value given the key. We can think of it as a set of <strong>key: value pairs<em> <\/em><\/strong>and every <strong>key<\/strong> in a dictionary is supposed to be <strong>unique<\/strong> so that we can access the corresponding <strong>values<\/strong> accordingly. <\/p>\n\n\n\n<p>A dictionary is denoted by the use of <strong>curly braces { }<\/strong> containing the key: value pairs. Each of the pairs in a dictionary is comma separated. The elements in a dictionary are <strong>un-ordered<\/strong> the sequence does not matter while we are accessing or storing them. <\/p>\n\n\n\n<p>They are <strong>MUTABLE<\/strong> which means that we can add, delete or update elements in a dictionary. Here are some code examples to get a better understanding of a dictionary in python.<\/p>\n\n\n\n<p>An important point to note is that we can&#8217;t use a mutable object as a key in the dictionary. So, a list is not allowed as a key in the dictionary.<\/p>\n\n\n\n<p><strong>1. Creating a Dictionary<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#creating a dictionary\n\nmy_dict = {\n    1:&#039;Delhi&#039;,\n    2:&#039;Patna&#039;,\n    3:&#039;Bangalore&#039;\n}\nprint(my_dict)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nOutput\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{1: &#039;Delhi&#039;, 2: &#039;Patna&#039;, 3: &#039;Bangalore&#039;}\n<\/pre><\/div>\n\n\n<p>Here, integers are the keys of the dictionary and the city name associated with integers are the values of the dictionary.  <\/p>\n\n\n\n<p><strong>2. Accessing items from a Dictionary<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#access an item\n\nprint(my_dict&#x5B;1])\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#039;Delhi&#039;\n<\/pre><\/div>\n\n\n<p><strong>3. Length of a Dictionary<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#length of the dictionary\n\nlen(my_dict)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n3\n<\/pre><\/div>\n\n\n<p><strong>4. Sorting a Dictionary<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#sorting based on the key \n\nPrint(sorted(my_dict.items()))\n\n\n#sorting based on the values of dictionary\n\nprint(sorted(my_dict.values()))\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;(1, &#039;Delhi&#039;), (2, &#039;Bangalore&#039;), (3, &#039;Patna&#039;)]\n\n&#x5B;&#039;Bangalore&#039;, &#039;Delhi&#039;, &#039;Patna&#039;]\n<\/pre><\/div>\n\n\n<p><strong>5.<\/strong> <strong>Adding elements in Dictionary <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#adding a new item in dictionary \n\nmy_dict&#x5B;4] = &#039;Lucknow&#039;\nprint(my_dict)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{1: &#039;Delhi&#039;, 2: &#039;Patna&#039;, 3: &#039;Bangalore&#039;, 4: &#039;Lucknow&#039;}\n<\/pre><\/div>\n\n\n<p><strong>6. Removing elements from Dictionary<\/strong> <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#for deleting an item from dict using the specific key\n\nmy_dict.pop(4)\nprint(my_dict)\n\n#for deleting last item from the list\n\nmy_dict.popitem()\n\n#for clearing the dictionary\n\nmy_dict.clear()\nprint(my_dict)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{1: &#039;Delhi&#039;, 2: &#039;Patna&#039;, 3: &#039;Bangalore&#039;}\n(3, &#039;Bangalore&#039;)\n{}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">SET<\/h2>\n\n\n\n<p>Set is another data type in python which is an unordered collection with no duplicate elements. Common use cases for a set are to remove duplicate values and to perform membership testing. <strong>Curly braces<\/strong> or the <strong><code>set()<\/code><\/strong> function can be used to create sets. One thing to keep in mind is that while creating an empty set, we have to use <strong><code>set()<\/code>, <\/strong>and<strong> <code>not { }<\/code>. The latter creates an empty dictionary.<\/strong><\/p>\n\n\n\n<p>Here are some code examples to get a better understanding of sets in python.<\/p>\n\n\n\n<p><strong>1. Creating a<\/strong> <strong>Set<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#creating set\n\nmy_set = {&quot;apple&quot;, &quot;mango&quot;, &quot;strawberry&quot;, &quot;apple&quot;}\nprint(my_set)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{&#039;apple&#039;, &#039;strawberry&#039;, &#039;mango&#039;}\n<\/pre><\/div>\n\n\n<p><strong>2. Accessing items from a Set<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#to test for an element inside the set\n\n&quot;apple&quot; in my_set\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">True<\/pre>\n\n\n\n<p><strong>3. Length of a Set<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(len(my_set))\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n3\n<\/pre><\/div>\n\n\n<p><strong>4. Sorting a Set<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(sorted(my_set))\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;&#039;apple&#039;, &#039;mango&#039;, &#039;strawberry&#039;]\n<\/pre><\/div>\n\n\n<p><strong>5.<\/strong> <strong>Adding elements in Set<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_set.add(&quot;guava&quot;)\nprint(my_set)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{&#039;apple&#039;, &#039;guava&#039;, &#039;mango&#039;, &#039;strawberry&#039;}\n<\/pre><\/div>\n\n\n<p><strong>6. Removing elements from Set<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_set.remove(&quot;mango&quot;)\nprint(my_set)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{&#039;apple&#039;, &#039;guava&#039;, &#039;strawberry&#039;}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we went through the most commonly used data structures in python and also saw various methods associated with them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Further Readings<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" data-type=\"post\" data-id=\"511\">List in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/dictionary\/python-dictionary-dict-tutorial\" data-type=\"post\" data-id=\"535\">Python Dictionary<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple\" data-type=\"post\" data-id=\"449\">Tuple in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/set\/python-set\" data-type=\"post\" data-id=\"1003\">Python Set<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p>Please check out the <a href=\"https:\/\/docs.python.org\/3.10\/tutorial\/datastructures.html\" target=\"_blank\" rel=\"noreferrer noopener\">official documentation for Python Data Structures<\/a> containing exhaustive information about the same.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In any programming language, we need to deal with data.&nbsp; Now, one of the most fundamental things that we need to work with the data is to store, manage, and access it efficiently in an organized way so it can be utilized whenever required for our purposes. Data Structures are used to take care of [&hellip;]<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-32781","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/32781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=32781"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/32781\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=32781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=32781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=32781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}