3

I have a simple for loop in a Python script:

for filename in filenames:
    outline = getinfo(filename)
    outfile.write(outline)

This for loop is part of a larger script that extracts data from HTML pages. I have nearly 6GB of HTML pages and want to do some test runs before I try it on all of them.

How can I make the loop break after a set number of iterations (lets say 100)?

2
  • Use a counter and break if that counter is equal to 100. Commented Jun 11, 2013 at 17:16
  • This should be a duplicate of stackoverflow.com/questions/2688079 but I ran out of close votes today. Commented Jul 30, 2022 at 3:49

4 Answers 4

13
for filename in filenames[:100]:
    outline= getinfo(filename)
    outfile.write(outline)

The list slice filenames[:100] will truncate the list of file names to just the first 100 elements.

Sign up to request clarification or add additional context in comments.

3 Comments

For a general iterator (not necessarily a list): for filename in itertools.islice(filenames, 100):.
@chepner Is that necessary also in Python 3?
Yes; you can't use slice syntax with arbitrary iterators.
9

Keep a counter for your for loop. When your counter reaches, 100, break

counter = 0
for filename in filenames:
    if counter == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)
    counter += 1

1 Comment

The preferred way to keep a counter is to do for (counter, filename) in enumerate(filenames).
2

I like @kqr's answer, but just another approach to consider, instead of taking the first 100, you could take a random n many instead:

from random import sample
for filename in sample(filenames, 10):
    # pass

3 Comments

I would argue this is the better solution as long as it doesn't have any terrible performance issues.
@kqr the main issue I would be worried about is reproduce-ability.... So maybe the compromise is to take 1 in n instead, which could be done nicely with slicing as shown in your answer... And still be more useful for testing...
Yes, that's what I thought too, but discarded since it would probably have similar performance to taking a random sample. I didn't think about testability, but you are indeed correct.
2

Use the built-in function enumerate(), available in both Python 2 and 3.

for idx,filename in enumerate(filenames):
    if idx == 100:
        break
    outline= getinfo(filename)
    outfile.write(outline)

Also look at this.

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.