1

I am trying to create an array, lets call it a, with each element equal to True with 10 elements

I also want to be able to create a custom index so that the first element index is 2 and the last element index is 11.

4
  • You probably mean a list object. In which case, that (nor anything that is called an array in python) doesn't let you use a custom index. You'll have to implement that yourself. Commented Jul 17, 2018 at 3:50
  • I have posted a solution to this question for future references, but as you are "very new to all types of programming" I would definitely not recommend you to start with pandas. Maybe you can post your actual problem and explain why you need custom indexes in the first place so we can guide in in some direction Commented Jul 17, 2018 at 3:55
  • I'm pretty sure there's a library on someone's blog that implements exactly what you're asking for, as a demonstration of how to create a MutableSequence class that delegates to a list but adds some extra functionality. Unfortunately, I don't have a link. Someone posted a link on my blog post demonstrating how to create a MutableSequence a few years back, but then deleted it… But anyway, building it yourself could be a valuable learning exercise, but maybe too advanced for right now. Commented Jul 17, 2018 at 3:55
  • Assuming that by "array" you mean list or tuple, and that you don't need to insert or delete elements after creation, you can simulate this with a dictionary: dictarray = {i+2: array[i] for i in range(len(array))}. It's probably not a great idea (you end up with something that acts like a list but with some functionality broken, and that runs slower and doesn't print out as nice), but it might be worth playing with to see how lists and dicts work. (You might want to write that out as an explicit loop instead of a dict comprehension, though, to make sure you understand it.) Commented Jul 17, 2018 at 3:59

1 Answer 1

2

You can make the array a dict instead.

a = {i: True for i in range(2, 12)}
print(a)
print(a[2], a[3], a[11], len(a))

This outputs:

{2: True, 3: True, 4: True, 5: True, 6: True, 7: True, 8: True, 9: True, 10: True, 11: True}
True True True 10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.