You don't have that choice. Python indexing starts at 0, and is not configurable.
You can just subtract 1 from your indices when indexing:
array.insert(i - 1, element) # but better just use array.append(element)
print(i, array[i - 1])
or (more wasteful), start your list with a dummy value at index 0:
array = [None]
at which point the next index used will be 1.
Answer from Martijn Pieters on Stack OverflowIs there a specific reason it was made this way? Personally, it gets confusing when I have to remember that the first character of a string has an index of 0...
The index method does not do what you expect. To get an item at an index, you must use the [] syntax:
>>> my_list = ['foo', 'bar', 'baz']
>>> my_list[1] # indices are zero-based
'bar'
index is used to get an index from an item:
>>> my_list.index('baz')
2
If you're asking whether there's any way to get index to recurse into sub-lists, the answer is no, because it would have to return something that you could then pass into [], and [] never goes into sub-lists.
list is an inbuilt function don't use it as variable name it is against the protocol instead use lst.
To access a element from a list use [ ] with index number of that element
lst = [1,2,3,4]
lst[0]
1
one more example of same
lst = [1,2,3,4]
lst[3]
4
Use (:) semicolon to access elements in series first index number before semicolon is Included & Excluded after semicolon
lst[0:3]
[1, 2, 3]
If index number before semicolon is not specified then all the numbers is included till the start of the list with respect to index number after semicolon
lst[:2]
[1, 2]
If index number after semicolon is not specified then all the numbers is included till the end of the list with respect to index number before semicolon
lst[1:]
[2, 3, 4]
If we give one more semicolon the specifield number will be treated as steps
lst[0:4:2]
[1, 3]
This is used to find the specific index number of a element
lst.index(3)
2
This is one of my favourite the pop function it pulls out the element on the bases of index provided more over it also remove that element from the main list
lst.pop(1)
2
Now see the main list the element is removed..:)
lst
[1, 3, 4]
For extracting even numbers from a given list use this, here i am taking new example for better understanding
lst = [1,1,2,3,4,44,45,56]
import numpy as np
lst = np.array(lst)
lst = lst[lst%2==0]
list(lst)
[2, 4, 44, 56]
For extracting odd numbers from a given list use this (Note where i have assingn 1 rather than 0)
lst = [1,1,2,3,4,44,45,56]
import numpy as np
lst = np.array(lst)
lst = lst[lst%2==1]
list(lst)
[1, 1, 3, 45]
Happy Learning...:)
If you really want to do this, you can create a class that wraps a list, and implement __getitem__ and __setitem__ to be one based. For example:
def __getitem__(self, index):
return self.list[index-1]
def __setitem__(self, index, value):
self.list[index-1] = value
However, to get the complete range of flexibility of Python lists, you would have to implement support for slicing, deleting, etc. If you just want a simple view of the list, one item at a time, this should do it for you.'
See Python Documentation for Data Model for more information on creating custom classes that act like sequences or mappings.
+1 to Max. Here's something else you could try:
Just add a None at index 0 of your list. This should get you the functionality. You'll just need to remember to snip out the leading None when you pass your list off to MATLAB
To explain it in another way, because -0 is equal to 0, if backward starts from 0, it is ambiguous to the interpreter.
If you are confused about -, and looking for another way to index backwards more understandably, you can try ~, it is a mirror of forward:
arr = ["a", "b", "c", "d"]
print(arr[~0]) # d
print(arr[~1]) # c
The typical usages for ~ are like "swap mirror node" or "find median in a sort list":
"""swap mirror node"""
def reverse(arr: List[int]) -> None:
for i in range(len(arr) // 2):
arr[i], arr[~i] = arr[~i], arr[i]
"""find median in a sort list"""
def median(arr: List[float]) -> float:
mid = len(arr) // 2
return (arr[mid] + arr[~mid]) / 2
"""deal with mirror pairs"""
# verify the number is strobogrammatic, strobogrammatic number looks the same when rotated 180 degrees
def is_strobogrammatic(num: str) -> bool:
return all(num[i] + num[~i] in '696 00 11 88' for i in range(len(num) // 2 + 1))
~ actually is a math trick of inverse code and complement code, and it is more easy to understand in some situations.
Discussion about whether should use python tricks like ~:
In my opinion, if it is a code maintained by yourself, you can use any trick to avoid potential bug or achieve goal easier, because of maybe a high readability and usability. But in team work, avoid using 'too clever' code, may bring troubles to your co-workers.
For example, here is one concise code from Stefan Pochmann to solve this problem. I learned a lot from his code. But some are just for fun, too hackish to use.
# a strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down)
# find all strobogrammatic numbers that are of length = n
def findStrobogrammatic(self, n):
nums = n % 2 * list('018') or ['']
while n > 1:
n -= 2
# n < 2 is so genius here
nums = [a + num + b for a, b in '00 11 88 69 96'.split()[n < 2:] for num in nums]
return nums
I have summarized python tricks like this, in case you are interested.
list[-1]
Is short hand for:
list[len(list)-1]
The len(list) part is implicit. That's why the -1 is the last element. That goes for any negative index - the subtraction from len(list) is always implicit