You are currently viewing How to implement __getitem__, __setitem__, and __delitem__ in Python

How to implement __getitem__, __setitem__, and __delitem__ in Python

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 __init__ which is used in Python classes to create and initialize objects.

In this article, we’ll see the usage and implementation of the underutilized dunder methods such as __getitem____setitem__, and __delitem__ in Python.


The name __getitem__ depicts that this method is used to access the items from the list, dictionary, and array.

If we have a list of names and want to access the item on the third index, we will use name_list[3], which will return the name from the list on the third index. When the name_list[3] is evaluated, Python internally calls __getitem__ on the data (name_list.__getitem__(3)).

The following example shows us the practical demonstration of the above theory.

We used the commonly used bracket notation to access the items from the my_list at the 0th and 2nd index and then to access the items at the 1st and 3rd index, we implemented the __getitem__ method.


__getitem__(self, key)

The __getitem__ is used to evaluate the value of self[key] by the object or instance of the class. Just like we saw earlier, object[key] is equivalent to object.__getitem__(key).

self – object or instance of the class

key – value we want to access

__getitem__ in Python classes

We created a Python class named Products and then defined the __getitem__ method to print the items. Then we created an instance of the class called item and then passed the values.

These values are of various data types and were actually parsed, for example, item['RAM', 'ROM'] was parsed as a tuple and this expression was evaluated by the interpreter as item.__getitem__(('RAM', 'ROM')).

Checking the type of the item along with the items.



In the following example, we created a class called Products, an __init__ that takes items and a price, and a __getitem__ that prints the value and type of the value passed inside the indexer.

Then we instantiated the class Products and passed the arguments 'Pen' and 10 to it, which we saved inside the obj. Then, using the instance obj, we attempted to obtain the values by accessing the parameters items and price.



The __setitem__ 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.

Here’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 (my list[1] = 'Yogesh'), and then printed the updated list.

To demonstrate what the interpreter does internally, we modified the list with the help of __setitem__.

When we run the above code, we’ll get the following output.


__setitem__(self, key, value)

The __setitem__ assigns a value to the key. If we call self[key] = value, then it will be evaluated as self.__setitem__(key, value).

self – object or instance of the class

key – the item that will be replaced

value – key will be replaced by this value

__setitem__ in Python classes

The following example demonstrates the implementation of the __setitem__ method in a Python class.

We created a Roles class and a __init__ function, passing the role and name parameters and storing them in a dictionary.

Then we defined the __getitem__ method, which returns the key’s value, and the getrole() function, which accesses the value passed to the key name and role.

Similarly, we defined the __setitem__ method, which assigns a value to the key, and we created the setrole() function, which assigns the specified values to the key role and name.

The class Roles('Python dev,' 'Sachin') was then instantiated with required arguments and stored inside the data object. We printed the getrole() function to get the role and name, then we called the setrole() function twice, passing it the various roles and names, and printing the getrole() function for each setrole() function we defined.

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.


The __delitem__ method deletes the items in the list, dictionary, or array. The item can also be deleted using the del keyword.

In the above code, we specified the del keyword and then specified the index number of the item to be deleted from my_list.

So, when we call del my_list[0] which is equivalent to del self[key], Python will call my_list.__delitem__(0) which is equivalent to self.__delitem__(key).

__delitem__ in Python class

We defined the delname function in the preceding code, which takes a key and deletes that entry from the dictionary created inside the __init__ function, as well as the setname function, which modifies/adds the entry to the dictionary.

Then we instantiated the Friends class, passed in the necessary arguments, and stored them in an instance called friends.

Then we used the delname function to remove an entry with the key name3 before printing the updated dictionary. In the following block, we modified the entry with the key name2 to demonstrate the functionality of setname function and printed the modified dictionary, then we deleted the entry with the key name2 and printed the updated dictionary.


We learned about the __getitem____setitem__, and __delitem__ methods in this article. We can compare __getitem__ to a getter function because it retrieves the value of the attribute, __setitem__ to a setter function because it sets the value of the attribute, and __delitem__ to a deleter function because it deletes the item.

We implemented these methods within Python classes in order to better understand how they work.

We’ve seen code examples that show what Python does internally when we access, set, and delete values.

🏆Other articles you might be interested in if you liked this one

How to use and implement the __init__ and __call__ in Python.

Types of class inheritance in Python with examples.

How underscores modify accessing the attributes and methods in Python.

Create and manipulate the temporary file in Python.

Display the static and dynamic images on the frontend using FastAPI.

Python one-liners to boost your code.

Perform a parallel iteration over multiple iterables using zip() function in Python.

That’s all for now

Keep Coding✌✌