Vicheanak
Vicheanak

Reputation: 6694

How to Skip Numbers In Each Iteration Loop String in Python?

I'm very new to this: but I have this string:

urls = ['http://example.com/page_%s.html' % page for page in xrange(0,50)]

which runs from 0,1,2,3 ... 50.

The question is how can i make run by skipping 5 number in each iteration?

The number should run like this: 0, 5, 10, 15 ... 50.

Upvotes: 0

Views: 253

Answers (2)

Noctis Skytower
Noctis Skytower

Reputation: 22041

You can try this:

urls = map('http://example.com/page_{}.html'.format, range(0, 50, 5))

range and xrange take an optional step argument as the third parameter.

Upvotes: 3

soohoonigan
soohoonigan

Reputation: 2360

Just adding the 5 as another argument to xrange should do that

urls = ['http://example.com/page_%s.html' % page for page in xrange(0,50,5)]

Upvotes: 3

Related Questions