Python Forum
knowing for loop position in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
knowing for loop position in a list
#1
I've got a text variable, my_word.
I want to run a for loop

For letter in my_word:
if letter == "f"

I want to know the index number of the letter that's being tested to see whether it's an f

Thanks.
Reply
#2
Look up the built-in enumerate function.

(I'm on a phone, so I apologise for the terse reply!)
Reply
#3
my_words = """I've got a text variable, my_word.
I want to run a for loop

For letter in my_word:
if letter == "f"

I want to know the index number of the letter that's being tested to see whether it's an f"""


for index, letter in enumerate(my_words):
    if letter == "f":
        print(index)
Output:
51 85 98 135 191
Reply
#4
Or, do what enumerate does behind the scenes explicitly:

myword = 'abcdefghf_fyou_mate'

findex = -1
for letter in myword:
    findex +=1
    if letter == 'f':
        print(f'This letter f has the list index {findex}.')
Reply
#5
(Jan-30-2025, 07:39 PM)medic5678 Wrote: For letter in my_word:
if letter == "f"

I want to know the index number of the letter that's being tested to see whether it's an f

As written it does make little sense. Letter passing if letter == "f" is "f", there is no need to use index to doublecheck whether its an f. If you don't trust Python to operate correctly while executing conditional then there is also no certainty in results of testing while using index number.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  partial functions before knowing the values mikisDeWitte 4 1,608 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Replace for loop to search index position illmattic 5 2,267 Sep-03-2022, 04:04 PM
Last Post: illmattic
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 2,586 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Appending to list of list in For loop nico_mnbl 2 3,032 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  list1 position = list 2? Nickd12 4 2,926 Sep-20-2020, 04:25 AM
Last Post: Nickd12
  Append list into list within a for loop rama27 2 4,151 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  find file not knowing the extension Leon79 6 4,552 Jul-07-2020, 04:44 PM
Last Post: Leon79
  Knowing the index of a data frame Ivannovix 1 2,384 May-01-2020, 02:51 PM
Last Post: klllmmm
  item = index position - list of list RavCOder 9 5,855 Dec-02-2019, 05:24 PM
Last Post: ThomasL
  CPython: Knowing when the interpreter expects more input b0bh00d 1 2,719 Apr-18-2019, 05:08 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020