Python Forum
Replace for loop to search index position
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace for loop to search index position
#1
Hi

I have following code which searches for index position of matching string without having to match everything:

sheet_num = []
for x in range(len(list_of_sheets)):
    if sheet_match in list_of_sheets[x]:
            sheet_num.append(x)
I'm looking for a better way to accomplish this as my code is filled with many occurrences of this type of for loop appending to an empty list.

I thought about:

list(map(lambda x: sheet_match in x, list_of_sheets))
But it returns a list of boolean values instead of the index position of the match. I'm sure its simple but I'm not so competent in Python to know it.

Thanks
Matt
Reply


Messages In This Thread
Replace for loop to search index position - by illmattic - Sep-03-2022, 10:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  knowing for loop position in a list medic5678 4 652 Jan-31-2025, 04:19 PM
Last Post: perfringo
  Running search/replace across Polars dataframe columns efficiently hobbycoder 3 2,056 Oct-28-2024, 03:18 AM
Last Post: hobbycoder
  Basic binary search algorithm - using a while loop Drone4four 1 2,286 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  For loop index out of bounds armitron121 2 4,055 Feb-08-2022, 04:23 PM
Last Post: armitron121
  Cloning a directory and using a .CSV file as a reference to search and replace bg25lam 2 2,830 May-31-2021, 07:00 AM
Last Post: bowlofred
  Duplicating Rows And Put Them In Certain Index Position Query eddywinch82 0 1,740 Nov-25-2020, 05:24 PM
Last Post: eddywinch82
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 3,757 May-10-2020, 05:28 AM
Last Post: deanhystad
  replace if\else with loop? mcmxl22 3 2,791 Mar-09-2020, 08:20 AM
Last Post: buran
  item = index position - list of list RavCOder 9 5,815 Dec-02-2019, 05:24 PM
Last Post: ThomasL
  Beginner problem, replace function with for loop Motley_Cow 9 6,168 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow

Forum Jump:

User Panel Messages

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