0

i have an array list, i find a point in the list i require and then take 3 values from that point right, however while this works perfect, i am worried due that if the value was the last value in the list it would error, instead i would want it to loop around to the start of the list again

for example if it picked z i would then want it to also pick a and b as well

this is my current code :

descriptor_position = bisect_left(orHashList, b32decode(descriptor_id,1)) #should be identiy list not HSDir_List #TODO - Add the other part of the list to it so it makes a circle
   for i in range(0,3):
      responsible_HSDirs.append(orHashList[descriptor_position+i])
   return (map(lambda x: consensus.get_router_by_hash(x) ,responsible_HSDirs))

what function or library can i use that achieves this ?

Thanks

1 Answer 1

4

You can generate the indices you want using a range, then wrap the indices round to the start of a list using the modulus % operator within a list comprehension - something like:

>>> a = ['a', 'b', 'c', 'd', 'e']
>>> index = 4
>>> [x % len(a) for x in range(index, index+3)]

[4, 0, 1]

>>> [a[x % len(a)] for x in range(index, index+3)]

['e', 'a', 'b']
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.