__getitem__ and __setitem__ in Python



In Python, you can customize many operations like accessing or modifying elements of a list or dictionary using special methods. Two important methods that make your objects behave like lists or dictionaries are:

  • __getitem__: called when you access an item using obj[key].
  • __setitem__: called when you assign a value using obj[key] = value.

These are examples of dunder methods (short for "double underscore"). They are special methods in Python with names that begin and end with double underscores. When you use square bracket syntax on your object:

  • obj[key] automatically triggers __getitem__
  • obj[key] = value automatically triggers __setitem__

By implementing these methods in your own classes, you can:

  • Make your objects act like lists or dictionaries
  • Support custom indexing and value assignment
  • Access or modify elements using the familiar obj[key] style

In this article, we will explore how __getitem__ and __setitem__ methods work through simple examples and see how you can use them to support custom indexing and value assignment.

Understanding __getitem__

The __getitem__ method is automatically called when you access an element from an object using square brackets. This lets you control how the object returns values for specific keys or indices.

Example: Reading Data with __getitem__

In this example, when we call my_list[1], the __getitem__ method is triggered, and it returns the value at index 1 from the internal list:

# Define a class that behaves like a list
class MyList:
   def __init__(self, data):
      self.data = data  # Store the data in a list

   def __getitem__(self, index):
      return self.data[index]  # Return the item at the given index

# Create an object with a list of numbers
my_list = MyList([1, 2, 3, 4, 5])

# Access an element using square brackets
print(my_list[1])

We get the output as shown below:

2

Understanding __setitem__

The __setitem__ method is used when you assign a value to an element using square brackets. This lets you define how new data should be stored or updated inside your object.

Example: Updating Data with __setitem__

In this example, my_list[1] = 42 triggers the __setitem__ method, which updates the value at index 1. Later, __getitem__ confirms the change:

# Define a class with both getitem and setitem
class MyList:
   def __init__(self, data):
      self.data = data  # Store the data in a list

   def __getitem__(self, index):
      return self.data[index]  # Return the item at the given index

   def __setitem__(self, index, value):
      self.data[index] = value  # Set the item at the given index

# Create an object and access its data
my_list = MyList([1, 2, 3, 4, 5])
print(my_list[1])   # Before updating

# Update the value at index 1
my_list[1] = 42
print(my_list[1])   # After updating

Following is the output obtained:

2
42

Conclusion

The __getitem__ and __setitem__ methods in Python let you decide how your custom objects handle getting and setting values with square brackets, like lists and dictionaries do.

When you use these methods in a right way, your code becomes cleaner and easier to read. It also makes your classes feel more natural to use in real-world programs.

Updated on: 2025-09-02T13:16:24+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements