How to remove strings that start with specific character from a list of strings in Python

2 Answers

0 votes
lst = ["python", "c++", "java", "php", "c#", "vb"] 
   
ch = 'p'
  
for s in lst[:]: 
    if s.startswith(ch): 
        lst.remove(s) 

print(lst) 
 
 
 
'''
run:
 
['c++', 'java', 'c#', 'vb']
 
'''

 



answered Jan 5, 2020 by avibootz
edited Jan 5, 2020 by avibootz
0 votes
lst = ["python", "c++", "java", "php", "c#", "vb"] 
   
ch = 'p'
  
lst = [s for s in lst if not s.startswith(ch)] 

print (lst) 
 
 
 
'''
run:
 
['c++', 'java', 'c#', 'vb']
 
'''

 



answered Jan 5, 2020 by avibootz
...