2

I have a start end, and a step, I would like to create a list like this (start= 0, end = 300, step = 100):

[[1, 100], [101, 200], [201, 300]]

The start, end, and step will vary, so I have to dynamically create the list.

4
  • Your list is inconsistent. You add 100 the first time, then 99 every other time.. Commented Nov 21, 2012 at 17:49
  • Should the first list be [1,100]? Commented Nov 21, 2012 at 17:50
  • Sorry guys, the start is 1, and the step in this sample is 99. It will vary. Commented Nov 21, 2012 at 17:51
  • 1
    @user1842734: Please edit your question to show the true input and output. Commented Nov 21, 2012 at 17:55

3 Answers 3

4
>>> start = 0
>>> end = 300
>>> step = 100
>>> [[1 + x, step + x] for x in range(start, end, step)]
[[1, 100], [101, 200], [201, 300]]
Sign up to request clarification or add additional context in comments.

1 Comment

black_dragon this is exactly what I need. Thank you!
1

You just need a simple while loop: -

start = 0
end = 300
step = 100

my_list = []

while start < end:   # Loop until you reach the end
    my_list.append([start + 1, start + step]) 
    start += step    # Increment start by step value to consider next group

print my_list

OUTPUT : -

[[1, 100], [101, 200], [201, 300]]

The same thing can be achieved by range or xrange function, in a list comprehension.

1 Comment

Thank you all! You have been awesome! :-)
1

You can create two ranges and zip them together:

def do_your_thing(start, end, step):
    return zip(range(start, end - step + 2, step),
               range(start + step - 1, end + 1, step))

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.