In Python, the index of an array (or list) starts from 0 by default. However, in some situations, you may want to start the index from 1—for example, when you work with matrices or when the indices represent a real-world quantity.
In this tutorial, we’ll show you how to start an array index from 1 in Python by creating a custom data structure with your desired indexing behavior.
Step 1: Create a Custom List Class
We’ll start by creating a custom class that inherits from Python’s native list class. This custom class will be used to initialize your desired index offset (in this case, 1).
1 2 3 4 |
class IndexList(list): def __init__(self, start_index, *args): self.start_index = start_index super(IndexList, self).__init__(*args) |
In the code snippet above, we define a class IndexList that inherits from Python’s native list class. The constructor method __init__
takes the start_index
parameter, which defines the desired start index for the list. We are using super()
to call the parent list class constructor method (__init__
) with the arguments passed.
Step 2: Overriding Indexing Methods
Next, we need to override the native Python list indexing methods to incorporate our custom index offset. In this step, we’ll override methods __getitem__
and __setitem__
:
1 2 3 4 5 6 7 8 |
class IndexList(list): # The existing constructor method from Step 1 def __getitem__(self, index): return super(IndexList, self).__getitem__(index - self.start_index) def __setitem__(self, index, value): super(IndexList, self).__setitem__(index - self.start_index, value) |
Here, __getitem__
and __setitem__
are methods that are internally handling the list indexing behavior. By overriding them, we are modifying the index value with our custom offset before passing it to the respective parent methods using super()
.
Step 3: Test the Custom List Class
Now let’s create an instance of our custom IndexList class to see if it works as expected:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def main(): my_list = IndexList(1, [10, 20, 30, 40, 50]) print(f"Original List: {my_list}") # Accessing elements using custom index print(f"Element at index 1: {my_list[1]}") print(f"Element at index 5: {my_list[5]}") # Modifying elements using custom index my_list[1] = 99 my_list[5] = 100 print(f"Modified List: {my_list}") if __name__ == "__main__": main() |
In this code, we create an instance of IndexList with a starting index of 1 and a given list of elements. We then print the elements of the list using our custom indexing and modify some values before printing the updated list.
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class IndexList(list): def __init__(self, start_index, *args): self.start_index = start_index super(IndexList, self).__init__(*args) def __getitem__(self, index): return super(IndexList, self).__getitem__(index - self.start_index) def __setitem__(self, index, value): super(IndexList, self).__setitem__(index - self.start_index, value) def main(): my_list = IndexList(1, [10, 20, 30, 40, 50]) print(f"Original List: {my_list}") # Accessing elements using custom index print(f"Element at index 1: {my_list[1]}") print(f"Element at index 5: {my_list[5]}") # Modifying elements using custom index my_list[1] = 99 my_list[5] = 100 print(f"Modified List: {my_list}") if __name__ == "__main__": main() |
Output
Original List: [10, 20, 30, 40, 50] Element at index 1: 10 Element at index 5: 50 Modified List: [99, 20, 30, 40, 100]
Conclusion
In this tutorial, we demonstrated how to start an array index from 1 in Python by creating a custom list class that inherits from Python’s native list class and overrides its indexing methods. This custom class will allow you to use lists with custom indexing and provide better control while working with data structures.