Given the following lists in Python:
l1 = [0,1000,5000,10000,20000,30000,40000,50000] #8 values, 7 intervals v1 = [1,2,3,4,5,6,7] #7 values v2 = [a,b,c,d,e,f,g] #7 letters
I want to check whether a number is contained in any interval, and return another value based on which interval this is.
1. My test value is 1111 2. It belongs to the second interval: 1000 < 1111 < 5000 3. Hence I need to return b
I would address the problem by:
- Creating chunks of
- Iterating over each chunk
- Writing one if statement for each chunk
- Return the letter corresponding to the correct chunk
I can create chunks of it by looking at every consecutive pair of numbers:
def chunker(seq, size): return (seq[pos:pos + size] for pos in range(0, len(seq))) for group in chunker(l1, 2): print(group)
[0, 1000] [1000, 5000] [5000, 10000] [10000, 20000] [20000, 30000] [30000, 40000] [40000, 50000] 
- Is there a Pythonic way of writing those if statements instead of having one for each chunk? What if I have 1,000 chunks?
- If there is, how to deal with the last chunk without creating a specific case for it?