Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to find the index of elements in a list based on the substring in the elements in Python

3 Answers

0 votes
a_list = ["bbc", "nnd", "wbb", "ccb", "qqd", "bbbbbo"]

index = [i for i, s in enumerate(a_list) if 'bb' in s]
 
print(index)
 
 
'''
run:
 
[0, 2, 5]
 
'''

 



answered Oct 27, 2017 by avibootz
edited Aug 18, 2024 by avibootz
0 votes
a_list = ["bbc", "nnd", "wbb", "ccb", "qqd", "bbbbbo"]

index = []
for i, elem in enumerate(a_list):
    if 'bb' in elem:
        index.append(i)
 
print(index)
 
 
'''
run:
 
[0, 2, 5]
 
'''

 



answered Oct 27, 2017 by avibootz
edited Aug 18, 2024 by avibootz
0 votes
a_list = ["bbc", "nnd", "wbb", "ccb", "qqd", "bbbbbo", "bbbb", "bbb", "bb", "b", "bxb"]

index = [a_list.index(i) for i in a_list if 'bb' in i]

print(index)
 
 
'''
run:
 
[0, 2, 5, 6, 7, 8]
 
'''

 



answered Oct 27, 2017 by avibootz
edited Aug 18, 2024 by avibootz
...