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
#2
Post working code sample that we can run,also you need to show what list_of_sheets and sheet_match is.
Reply
#3
matching_sheet_indexes = [
    index for index, sheet in enumerate(list_of_sheets) if sheet_match in sheet
]
Reply
#4
(Sep-03-2022, 11:37 AM)Yoriz Wrote:
matching_sheet_indexes = [
    index for index, sheet in enumerate(list_of_sheets) if sheet_match in sheet
]

Thank you! This did the trick. I was playing around with enumerate but couldn't get it to work as I'm not familiar with the 'index for index' part. Can I ask more about the code? What does this 'index for index' exactly do as the word 'index' is not called again? I suppose its iterating through the index of list_of_sheets but can you point me to something that clarify for a novice whats exactly happening here. Thanks
Reply
#5
List comprehension. A shorthand way of writing a for loop that builds a list.
matching_sheet_indexes = []
for index, sheet in enumerate(list_of_sheets):
    if sheet_match in sheet:
        matching_sheet_indexes.append[index]
There are also dictionary, set and generator comprehensions.
Reply
#6
(Sep-03-2022, 02:41 PM)deanhystad Wrote: List comprehension. A shorthand way of writing a for loop that builds a list.
matching_sheet_indexes = []
for index, sheet in enumerate(list_of_sheets):
    if sheet_match in sheet:
        matching_sheet_indexes.append[index]
There are also dictionary, set and generator comprehensions.

Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic binary search algorithm - using a while loop Drone4four 1 377 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  For loop index out of bounds armitron121 2 2,689 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,150 May-31-2021, 07:00 AM
Last Post: bowlofred
  Duplicating Rows And Put Them In Certain Index Position Query eddywinch82 0 1,289 Nov-25-2020, 05:24 PM
Last Post: eddywinch82
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 2,896 May-10-2020, 05:28 AM
Last Post: deanhystad
  replace if\else with loop? mcmxl22 3 2,152 Mar-09-2020, 08:20 AM
Last Post: buran
  item = index position - list of list RavCOder 9 4,157 Dec-02-2019, 05:24 PM
Last Post: ThomasL
  Beginner problem, replace function with for loop Motley_Cow 9 4,657 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow
  Get all values of for loop with an index BollerwagenIng 2 2,508 Aug-09-2019, 07:58 AM
Last Post: BollerwagenIng
  Index error using pop in nested loop PerksPlus 3 3,420 Mar-28-2019, 03:11 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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