LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2008, 02:10 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
python: can I see which iteration I'm on in a loop?


In this bit of code, is there a way to do away with the "count" var?

Code:
count = 0
for root,dirs,files in os.walk("/some/dir"):
    count += 1
    if (count % 10 == 0):
        do_something()
I think I remembered seeing some special built-in that you can ask which index of the iteration you're on rather than maintaining a "count" variable. Is that correct?
 
Old 08-27-2008, 08:44 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Yes. You just do

Code:
for index, value in enumerate(the_iterable):
 
Old 08-27-2008, 09:01 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by Dan04 View Post
Yes. You just do

Code:
for index, value in enumerate(the_iterable):
that's what I was looking for.

Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop iteration in Linux scripting. 151803 Linux - Newbie 3 03-19-2007 06:36 AM
force grep to keep it's place in file for next iteration? jeffreybluml Programming 7 05-13-2005 10:16 AM
pronounce 'iteration' Ikebo General 5 09-28-2004 07:25 PM
newbie python question: doing more than one thing in a for loop SerfurJ Programming 1 03-18-2004 10:14 AM
Python - Set vars and loop over lines in file jnoller Programming 6 02-07-2004 10:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration