Python Forum
How to change 0 based indexing to 1 based indexing in python..??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change 0 based indexing to 1 based indexing in python..??
#1
Python works in 0 based index. I have tried to change the python to work in 1 based index.

python_file_A.py
def __getitem__(self, index):
  return self.list[index-1]
def __setitem__(self, index, value):
  self.list[index-1] = value
python_file_B.py
example_list=['a','b','c','d','e']
print("Before function change : ",example_list[1])
from python_file_A import *
print("After function change : ",example_list[1])
While running python_file_B.py,
Actual Output :
Before function change : b
After function change : b

Expected Output :
Before function change : b
After function change : a

After function change the example_list[1] should give 'a' as output which is example_list[0]
Without using class be preferred. Kindly help to resolve this.

reference links below,
https://stackoverflow.com/questions/1172...ex-0-based
http://defcraft.blogspot.com/2007/04/n-b...-list.html
Reply
#2
For a function not in a class, remove the reference to self.

General recommendation - wouldn't it be better to just remember that it is zero based? Using getters and setters is definitely un-pythonic, adds a layer for errors to creep in.
Reply
#3
(Jan-22-2020, 05:07 PM)jefsummers Wrote: For a function not in a class, remove the reference to self.

General recommendation - wouldn't it be better to just remember that it is zero based? Using getters and setters is definitely un-pythonic, adds a layer for errors to creep in.

python_file_A.py
def __getitem__(index):
  return list[index-1]
def __setitem__(index, value):
  list[index-1] = value
python_file_B.py
example_list=['a','b','c','d','e']
print("Before function change : ",example_list[1])
from python_file_A import *
print("After function change : ",example_list[1])
While running python_file_B.py,
Actual Output :
Before function change : b
After function change : b

Expected Output :
Before function change : b
After function change : a

Removed the self (Actually self is not a real issue what i am seeking for). Even though the expected output is not coming. You can recommend other ways of doing it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python class members based on a type value voidtrance 7 1,237 Apr-11-2025, 10:10 PM
Last Post: deanhystad
  Create a new subclass in a Python extension based on an existing class voidtrance 6 1,380 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  Python-based telegram spam bot-telegram-bot archer24 1 616 Feb-07-2025, 04:16 PM
Last Post: archer24
  Changing client.get() method type based on size of data... dl0dth 1 717 Jan-02-2025, 08:30 PM
Last Post: dl0dth
  Finding the price based on industry and number of transactions chandramouliarun 1 1,700 Jun-04-2024, 06:57 PM
Last Post: marythodge4
  plotting based on the results of mathematical formulas Timur 1 929 Feb-08-2024, 07:22 PM
Last Post: Gribouillis
  Copy Paste excel files based on the first letters of the file name Viento 2 1,588 Feb-07-2024, 12:24 PM
Last Post: Viento
  unable to remove all elements from list based on a condition sg_python 3 1,694 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Create dual folder on different path/drive based on the date agmoraojr 2 1,356 Jan-21-2024, 10:02 AM
Last Post: snippsat
  Searche each element of each tuple based 3 numbes zinho 8 2,223 Dec-11-2023, 05:14 PM
Last Post: zinho

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020