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


Messages In This Thread
How to change 0 based indexing to 1 based indexing in python..?? - by Ruthra - Jan-22-2020, 04:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  plotting based on the results of mathematical formulas Timur 1 352 Feb-08-2024, 07:22 PM
Last Post: Gribouillis
  Copy Paste excel files based on the first letters of the file name Viento 2 455 Feb-07-2024, 12:24 PM
Last Post: Viento
  unable to remove all elements from list based on a condition sg_python 3 457 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Create dual folder on different path/drive based on the date agmoraojr 2 456 Jan-21-2024, 10:02 AM
Last Post: snippsat
  Searche each element of each tuple based 3 numbes zinho 8 890 Dec-11-2023, 05:14 PM
Last Post: zinho
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 720 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Move Files based on partial Match mohamedsalih12 2 833 Sep-20-2023, 07:38 PM
Last Post: snippsat
  Python Launcher Pops Up When Py-based App Is Running (Mac) radix_optimus 0 551 Sep-18-2023, 09:22 AM
Last Post: radix_optimus
  How to do 100 runs simulation based on the current codes? dududada 6 999 Sep-03-2023, 01:43 PM
Last Post: deanhystad
  Color a table cell based on specific text Creepy 11 2,012 Jul-27-2023, 02:48 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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