I have an alphabets list:
alpha_list = ['a', 'b', 'c', 'd', 'e']
For a given alphabet(considering it will always be present in alpha_list) I want to get an alphabet whose index is garter by a given number, consider below function for example:
def get_replacing_letter(alphabet, number):
index = alpha_list.index(alphabet)
return alpha_list[index number]
get_replacing_letter('a', 2)
will give me 'c'
what I want is get_replacing_letter('d', 2)
should give 'a'
similarly get_replacing_letter('e', 2)
should give 'b'
So the alph_list
should work in a chaining sequence or cyclic manner. I am wondering how to achieve this in Python?
CodePudding user response:
You can make new index take the modulo of the length of the list:
return alpha_list[(index number) % len(alpha_list)]
CodePudding user response:
At the moment I achieved it by extending the alpha_list with itself:
alpha_list.extend(alpha_list)
This way as index method gives first index of occurance, I could manage to get it working.
However I am wondering if there is any better way to achieve this.