python - Repeat list if index range is out of bounds -


i have python list

a = [1, 2, 3, 4] 

and i'd range of indices such if select indices 0 through n, i'm getting (for n=10) repeated

[1, 2, 3, 4, 1, 2, 3, 4, 1, 2] 

i of course repeat list via (int(float(n) / len(a) - 0.5) + 1) * a first , select range [0:10] out of that, feels rather clumsy.

any hints?

you can use modulo operator when accessing list, i.e.

a[i % len(a)] 

this give same result, doesn't require store redundant elements.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -