{"id":1029,"date":"2023-03-16T20:40:00","date_gmt":"2023-03-16T15:10:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=1029"},"modified":"2023-09-25T11:11:17","modified_gmt":"2023-09-25T05:41:17","slug":"implement-getitem-setitem-and-delitem-in-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python","title":{"rendered":"How to implement __getitem__, __setitem__, and __delitem__ in Python"},"content":{"rendered":"\n<p>Python has numerous collections of dunder methods(which start with double underscores and end with double underscores) to perform various tasks. The most commonly used dunder method is&nbsp;<code>__init__<\/code>&nbsp;which is used in Python classes to create and initialize objects.<\/p>\n\n\n\n<p>In this article, we&#8217;ll see the usage and implementation of the underutilized dunder methods such as\u00a0<code>__getitem__<\/code>,\u00a0<code>__setitem__<\/code>, and\u00a0<code>__delitem__<\/code>\u00a0in Python.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<p class=\"responsive-video-wrap clr\"><iframe loading=\"lazy\" title=\"Python&#039;s __getitem__ Method Simplified for Object Element Access in 2 Minutes\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/tkZSjGMSFf8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe><\/p>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-getitem\"><a href=\"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python#heading-getitem\"><\/a><strong>__getitem<\/strong>__<\/h2>\n\n\n\n<p>The name&nbsp;<strong><code>__getitem__<\/code><\/strong>&nbsp;depicts that this method is used to access the items from the list, dictionary, and array.<\/p>\n\n\n\n<p>If we have a list of names and want to access the item on the third index, we will use\u00a0<code>name_list[3]<\/code>, which will return the name from the list on the third index. When the\u00a0<code>name_list[3]<\/code>\u00a0is evaluated, Python internally calls\u00a0<code>__getitem__<\/code>\u00a0on the data (<code>name_list.__getitem__(3)<\/code>).<\/p>\n\n\n\n<p>The following example shows us the practical demonstration of the above theory.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># List of names\nmy_list = ['Sachin', 'Rishu', 'Yashwant', 'Abhishek']\n\n# Accessing items using bracket notation\nprint('Accessed items using the bracket notation')\nprint(my_list[0])\nprint(my_list[2], \"\\n\")\n\n# Accessing items using __getitem__\nprint('Accessed items using the __getitem__')\nprint(my_list.__getitem__(1))\nprint(my_list.__getitem__(3))<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p>We used the commonly used bracket notation to access the items from the&nbsp;<code>my_list<\/code>&nbsp;at the&nbsp;<code>0th<\/code>&nbsp;and&nbsp;<code>2nd<\/code>&nbsp;index and then to access the items at the&nbsp;<code>1st<\/code>&nbsp;and&nbsp;<code>3rd<\/code>&nbsp;index, we implemented the&nbsp;<code>__getitem__<\/code>&nbsp;method.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Accessed items using the bracket notation\nSachin\nYashwant \n\nAccessed items using the __getitem__\nRishu\nAbhishek<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-syntax\"><a href=\"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python#heading-syntax\"><\/a>Syntax<\/h3>\n\n\n\n<p><code>__getitem__(self, key)<\/code><\/p>\n\n\n\n<p>The <code>__getitem__<\/code> is used to evaluate the value of&nbsp;<code>self[key]<\/code>&nbsp;by the object or instance of the class. Just like we saw earlier,&nbsp;<code>object[key]<\/code>&nbsp;is equivalent to&nbsp;<code>object.__getitem__(key)<\/code>.<\/p>\n\n\n\n<p><code>self<\/code>&nbsp;&#8211; object or instance of the class<\/p>\n\n\n\n<p><code>key<\/code>&nbsp;&#8211; value we want to access<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-getitem-in-python-classes\"><a href=\"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python#heading-getitem-in-python-classes\"><\/a>__getitem__ in Python classes<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Creating a class\nclass Products:\n    def __getitem__(self, items):\n        print(f'Item: {items}')\n\n\nitem = Products()\nitem['RAM', 'ROM']\nitem[{'Storage': 'SSD'}]\nitem['Graphic Card']<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p>We created a Python class named&nbsp;<code>Products<\/code>&nbsp;and then defined the&nbsp;<code>__getitem__<\/code>&nbsp;method to print the&nbsp;<code>items<\/code>. Then we created an instance of the class called&nbsp;<code>item<\/code>&nbsp;and then passed the values.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Item: ('RAM', 'ROM')\nItem: {'Storage': 'SSD'}\nItem: Graphic Card<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p>These values are of various data types and were actually parsed, for example,&nbsp;<code>item['RAM', 'ROM']<\/code>&nbsp;was parsed as a tuple and this expression was evaluated by the interpreter as&nbsp;<code>item.__getitem__(('RAM', 'ROM'))<\/code>.<\/p>\n\n\n\n<p>Checking the type of the item along with the items.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import math\n# Creating a class\nclass Products:\n    # Printing the types of item along with items\n    def __getitem__(self, items):\n        print(f'Item: {items}. Type: {type(items)}')\n\n\nitem = Products()\nitem['RAM', 'ROM']\nitem[{'Storage': 'SSD'}]\nitem['Graphic Card']\nitem[math]\nitem[89]<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Item: ('RAM', 'ROM'). Type: &lt;class 'tuple'&gt;\nItem: {'Storage': 'SSD'}. Type: &lt;class 'dict'&gt;\nItem: Graphic Card. Type: &lt;class 'str'&gt;\nItem: &lt;module 'math' (built-in)&gt;. Type: &lt;class 'module'&gt;\nItem: 89. Type: &lt;class 'int'&gt;<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-example\"><a href=\"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python#heading-example\"><\/a>Example<\/h3>\n\n\n\n<p>In the following example, we created a class called&nbsp;<code>Products<\/code>, an&nbsp;<code>__init__<\/code>&nbsp;that takes&nbsp;<code>items<\/code>&nbsp;and a&nbsp;<code>price<\/code>, and a&nbsp;<code>__getitem__<\/code>&nbsp;that prints the value and type of the value passed inside the indexer.<\/p>\n\n\n\n<p>Then we instantiated the class&nbsp;<code>Products<\/code>&nbsp;and passed the arguments&nbsp;<code>'Pen'<\/code>&nbsp;and&nbsp;<code>10<\/code>&nbsp;to it, which we saved inside the&nbsp;<code>obj<\/code>. Then, using the instance&nbsp;<code>obj<\/code>, we attempted to obtain the values by accessing the parameters&nbsp;<code>items<\/code>&nbsp;and&nbsp;<code>price<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Creating a class\nclass Products:\n    # Creating a __init__ function\n    def __init__(self, items, price):\n        self.items = items\n        self.price = price\n\n    def __getitem__(self, value):\n        print(value, type(value))\n\n# Creating instance of the class and passing the values\nobj = Products('Pen',10)\n# Accessing the values\nobj[obj.items]\nobj[obj.price]<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Pen &lt;class 'str'&gt;\n10 &lt;class 'int'&gt;<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-setitem\"><a href=\"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python#heading-setitem\"><\/a>__setitem__<\/h2>\n\n\n\n<p>The&nbsp;<code>__setitem__<\/code>&nbsp;is used to assign the values to the item. When we assign or set a value to an item in a list, array, or dictionary, this method is called internally.<\/p>\n\n\n\n<p>Here&#8217;s an example in which we created a list of names, and attempted to modify the list by changing the name at the first index (<code>my list[1] = 'Yogesh'<\/code>), and then printed the updated list.<\/p>\n\n\n\n<p>To demonstrate what the interpreter does internally, we modified the list with the help of&nbsp;<code>__setitem__<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># List of names\nmy_list = ['Sachin', 'Rishu', 'Yashwant', 'Abhishek']\n\n# Assigning other name at the index value 1\nmy_list[1] = 'Yogesh'\nprint(my_list)\n\nprint('-'*20)\n\n# What interpreter does internally\nmy_list.__setitem__(2, 'Rishu')\nprint(my_list)<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p>When we run the above code, we&#8217;ll get the following output.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">['Sachin', 'Yogesh', 'Yashwant', 'Abhishek']\n--------------------\n['Sachin', 'Yogesh', 'Rishu', 'Abhishek']<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-syntax-1\"><a href=\"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python#heading-syntax-1\"><\/a>Syntax<\/h3>\n\n\n\n<p><code>__setitem__(self, key, value)<\/code><\/p>\n\n\n\n<p>The&nbsp;<code>__setitem__<\/code>&nbsp;assigns a value to the key. If we call&nbsp;<code>self[key] = value<\/code>, then it will be evaluated as&nbsp;<code>self.__setitem__(key, value)<\/code>.<\/p>\n\n\n\n<p><code>self<\/code>&nbsp;&#8211; object or instance of the class<\/p>\n\n\n\n<p><code>key<\/code>&nbsp;&#8211; the item that will be replaced<\/p>\n\n\n\n<p><code>value<\/code>&nbsp;&#8211;&nbsp;<code>key<\/code>&nbsp;will be replaced by this value<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-setitem-in-python-classes\"><a href=\"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python#heading-setitem-in-python-classes\"><\/a>__setitem__ in Python classes<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Creating a class\nclass Roles:\n    # Defining __init__ method\n    def __init__(self, role, name):\n        # Creating a dictionary with key-value pair\n        self.detail = {\n            'name': name,\n            'role': role\n        }\n\n    # Defining __getitem__ method\n    def __getitem__(self, key):\n        return self.detail[key]\n\n    # Function to get the role and name\n    def getrole(self):\n        return self.__getitem__('role'), self.__getitem__('name')\n\n    # Defining __setitem__ method\n    def __setitem__(self, key, value):\n        self.detail[key] = value\n\n    # Function to set the role and name\n    def setrole(self, role, name):\n        print(f'{role} role has been assigned to {name}.')\n        return self.__setitem__('role', role), self.__setitem__('name', name)\n\n# Instantiating the class with required args\ndata = Roles('Python dev', 'Sachin')\n# Printing the role with name\nprint(data.getrole())\n\n# Setting the role for other guys\ndata.setrole('C++ dev', 'Rishu')\n# Printing the assigned role with name\nprint(data.getrole())\n# Setting the role for other guys\ndata.setrole('PHP dev', 'Yashwant')\n# Printing the assigned role with name\nprint(data.getrole())<\/pre><\/div>\n\n\n\n<p>The following example demonstrates the implementation of the&nbsp;<code>__setitem__<\/code>&nbsp;method in a Python class.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p>We created a&nbsp;<code>Roles<\/code>&nbsp;class and a&nbsp;<code>__init__<\/code>&nbsp;function, passing the&nbsp;<code>role<\/code>&nbsp;and&nbsp;<code>name<\/code>&nbsp;parameters and storing them in a dictionary.<\/p>\n\n\n\n<p>Then we defined the&nbsp;<code>__getitem__<\/code>&nbsp;method, which returns the key&#8217;s value, and the&nbsp;<code>getrole()<\/code>&nbsp;function, which accesses the value passed to the key&nbsp;<code>name<\/code>&nbsp;and&nbsp;<code>role<\/code>.<\/p>\n\n\n\n<p>Similarly, we defined the&nbsp;<code>__setitem__<\/code>&nbsp;method, which assigns a value to the key, and we created the&nbsp;<code>setrole()<\/code>&nbsp;function, which assigns the specified values to the key&nbsp;<code>role<\/code>&nbsp;and&nbsp;<code>name<\/code>.<\/p>\n\n\n\n<p>The class&nbsp;<code>Roles('Python dev,' 'Sachin')<\/code>&nbsp;was then instantiated with required arguments and stored inside the&nbsp;<code>data<\/code>&nbsp;object. We printed the&nbsp;<code>getrole()<\/code>&nbsp;function to get the&nbsp;<strong>role<\/strong>&nbsp;and&nbsp;<strong>name<\/strong>, then we called the&nbsp;<code>setrole()<\/code>&nbsp;function twice, passing it the various&nbsp;<strong>roles<\/strong>&nbsp;and&nbsp;<strong>names<\/strong>, and printing the&nbsp;<code>getrole()<\/code>&nbsp;function for each&nbsp;<code>setrole()<\/code>&nbsp;function we defined.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">('Python dev', 'Sachin')\nC++ dev role has been assigned to Rishu.\n('C++ dev', 'Rishu')\nPHP dev role has been assigned to Yashwant.\n('PHP dev', 'Yashwant')<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p>We got the values passed as an argument to the class but after it, we set the different roles and names and got the output we expected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-delitem\"><a href=\"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python#heading-delitem\"><\/a>__delitem__<\/h2>\n\n\n\n<p>The&nbsp;<code>__delitem__<\/code>&nbsp;method deletes the items in the list, dictionary, or array. The item can also be deleted using the&nbsp;<code>del<\/code>&nbsp;keyword.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># List of names\nmy_list = ['Sachin', 'Rishu', 'Yashwant', 'Abhishek']\n\n# Deleting the first item of the list\ndel my_list[0]\nprint(my_list)\n\n# Deleting the item using __delitem__\nmy_list.__delitem__(1)\nprint(my_list)\n\n----------\n['Rishu', 'Yashwant', 'Abhishek']\n['Rishu', 'Abhishek']<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p>In the above code, we specified the&nbsp;<code>del<\/code>&nbsp;keyword and then specified the index number of the item to be deleted from&nbsp;<code>my_list<\/code>.<\/p>\n\n\n\n<p>So, when we call&nbsp;<code>del my_list[0]<\/code>&nbsp;which is equivalent to&nbsp;<code>del self[key]<\/code>, Python will call&nbsp;<code>my_list.__delitem__(0)<\/code>&nbsp;which is equivalent to&nbsp;<code>self.__delitem__(key)<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-delitem-in-python-class\"><a href=\"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python#heading-delitem-in-python-class\"><\/a><strong>__<\/strong>delitem__ in Python class<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">class Friends:\n    def __init__(self, name1, name2, name3, name4):\n        self.n = {\n            'name1': name1,\n            'name2': name2,\n            'name3': name3,\n            'name4': name4\n        }\n\n    # Function for deleting the entry\n    def delname(self, key):\n        self.n.__delitem__(key)\n\n    # Function for adding\/modifying the entry\n    def setname(self, key, value):\n        self.n[key] = value\n\n\nfriend = Friends('Sachin', 'Rishu', 'Yashwant', 'Abhishek')\nprint(friend.n, \"\\n\")\n\n# Deleting an entry\nfriend.delname('name3')\nprint('After deleting the name3 entry')\nprint(friend.n, \"\\n\")\n\n# Modifying an entry\nfriend.setname('name2', 'Yogesh')\nprint('name2 entry modified')\nprint(friend.n, \"\\n\")\n\n# Deleting an entry\nfriend.delname('name2')\nprint('After deleting the name2 entry')\nprint(friend.n)<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<p>We defined the&nbsp;<code>delname<\/code>&nbsp;function in the preceding code, which takes a&nbsp;<code>key<\/code>&nbsp;and deletes that entry from the dictionary created inside the&nbsp;<code>__init__<\/code>&nbsp;function, as well as the&nbsp;<code>setname<\/code>&nbsp;function, which modifies\/adds the entry to the dictionary.<\/p>\n\n\n\n<p>Then we instantiated the&nbsp;<code>Friends<\/code>&nbsp;class, passed in the necessary arguments, and stored them in an instance called&nbsp;<code>friends<\/code>.<\/p>\n\n\n\n<p>Then we used the&nbsp;<code>delname<\/code>&nbsp;function to remove an entry with the key&nbsp;<code>name3<\/code>&nbsp;before printing the updated dictionary. In the following block, we modified the entry with the key&nbsp;<code>name2<\/code>&nbsp;to demonstrate the functionality of&nbsp;<code>setname<\/code>&nbsp;function and printed the modified dictionary, then we deleted the entry with the key&nbsp;<code>name2<\/code>&nbsp;and printed the updated dictionary.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">{'name1': 'Sachin', 'name2': 'Rishu', 'name3': 'Yashwant', 'name4': 'Abhishek'} \n\nAfter deleting the name3 entry\n{'name1': 'Sachin', 'name2': 'Rishu', 'name4': 'Abhishek'} \n\nname2 entry modified\n{'name1': 'Sachin', 'name2': 'Yogesh', 'name4': 'Abhishek'} \n\nAfter deleting the name2 entry\n{'name1': 'Sachin', 'name4': 'Abhishek'}<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/implement-getitem-setitem-and-delitem-in-python#heading-conclusion\"><\/a>Conclusion<\/h2>\n\n\n\n<p>We learned about the&nbsp;<code>__getitem__<\/code>,&nbsp;<code>__setitem__<\/code>, and&nbsp;<code>__delitem__<\/code>&nbsp;methods in this article. We can compare&nbsp;<code>__getitem__<\/code>&nbsp;to a getter function because it retrieves the value of the attribute,&nbsp;<code>__setitem__<\/code>&nbsp;to a setter function because it sets the value of the attribute, and&nbsp;<code>__delitem__<\/code>&nbsp;to a deleter function because it deletes the item.<\/p>\n\n\n\n<p>We implemented these methods within Python classes in order to better understand how they work.<\/p>\n\n\n\n<p>We&#8217;ve seen code examples that show what Python does internally when we access, set, and delete values.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might be interested in if you liked this one<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/init-and-call-method\" rel=\"noreferrer noopener\">How to use and implement the __init__ and __call__ in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/class-inheritance-in-python\" rel=\"noreferrer noopener\">Types of class inheritance in Python with examples<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/access-modifiers-in-python\" rel=\"noreferrer noopener\">How underscores modify accessing the attributes and methods in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/tempfile-in-python\" rel=\"noreferrer noopener\">Create and manipulate the temporary file in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/displaying-images-on-the-frontend-using-fastapi\" rel=\"noreferrer noopener\">Display the static and dynamic images on the frontend using FastAPI<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/one-liners-in-python\" rel=\"noreferrer noopener\">Python one-liners to boost your code<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/zip-function-in-python-usage-and-examples-with-code\" rel=\"noreferrer noopener\">Perform a parallel iteration over multiple iterables using zip() function in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python has numerous collections of dunder methods(which start with double underscores and end with double underscores) to perform various tasks. The most commonly used dunder method is&nbsp;__init__&nbsp;which is used in Python classes to create and initialize objects. In this article, we&#8217;ll see the usage and implementation of the underutilized dunder methods such as\u00a0__getitem__,\u00a0__setitem__, and\u00a0__delitem__\u00a0in Python. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1031,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2],"tags":[12,31],"class_list":["post-1029","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=1029"}],"version-history":[{"count":6,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1029\/revisions"}],"predecessor-version":[{"id":1533,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/1029\/revisions\/1533"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/1031"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=1029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=1029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=1029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}