-2

I'm using Python 3.10. I have an incredibly large list. Each item in the list is 26 character string. Somewhere within this vast list is a name. I would like to return the list indices of all occurrences of this name, which is a substring within a string.

It's a simple list called alphabet, with just one entry at each index. Every entry is a string and what I'm looking for is a string. Everything is in uppercase and no special characters are involved.

I've looked at several things but I'm only able to find results for returning substrings within strings. It's the fact that it's in a list that is stumping me...

DanMack
  • 59
  • 6
  • 1
    An exact duplicate: [Find all index position in list based on partial string inside item in list](https://stackoverflow.com/q/14849293/6045800) – Tomerikoo Feb 24 '22 at 15:17
  • Just curious: how large is "incredibly" large, and what does the size matter? – Kelly Bundy Feb 24 '22 at 15:29
  • @Tomerikoo. You're right, I apologise. I'm still learning but need to be better with searches. My mistake. – DanMack Feb 25 '22 at 09:44

1 Answers1

3

Try this:

indices = [idx for idx, s in enumerate(alphabet) if name in s]

where alphabet is your list of strings, and name is the desired substring.

Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
  • Hi there. The name is a substring within a string... You answered before I could edit, I apologise. – DanMack Feb 24 '22 at 15:12