-3

So I have a list like below-

list = [scaler-1, scaler-2, scaler-3, backend-1, backend-2, backend-3]

I want to create another list from it with the words which starts with 'backend'.How can i do that ?

Please note the content of the list will change from system to system, I want my code to be dynamic, any help?

DD1
  • 357
  • 4
  • 14

1 Answers1

3

First off, do not use the name list for assignment to your objects, you'll shadow the builtin list type.

Then, you can use a list comprehension with str.startswith in a filter:

new_lst = [x for x in lst if x.startswith('backend')]
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139