6

I was wondering what's the most pythonic way to:

Having a list of strings and a list of substrings remove the elements of string list that contains any of the substring list.

list_dirs = ('C:\\foo\\bar\\hello.txt', 'C:\\bar\\foo\\.world.txt', 'C:\\foo\\bar\\yellow.txt')

unwanted_files = ('hello.txt', 'yellow.txt)

Desired output:

list_dirs = (C:\\bar\\foo\.world.txt')

I have tried to implement similar questions such as this, but I'm still struggling making the removal and extend that particular implementation to a list.

So far I have done this:

for i in arange(0, len(list_dirs)):
    if 'hello.txt' in list_dirs[i]:
        list_dirs.remove(list_dirs[i])

This works but probably it's not the more cleaner way and more importantly it does not support a list, if I want remove hello.txt or yellow.txt I would have to use a or. Thanks.

2
  • @PM 2Ring Yes I was using numpy. arange is similar to range Commented Feb 22, 2015 at 11:58
  • It's not safe to modify a collection that you're iterating over. Commented Feb 22, 2015 at 12:06

2 Answers 2

2

Using list comprehensions

>>> [l for l in list_dirs if l.split('\\')[-1] not in unwanted_files]
['C:\\bar\\foo\\.world.txt']

Use split to get filename

>>> [l.split('\\')[-1] for l in list_dirs]
['hello.txt', '.world.txt', 'yellow.txt']
Sign up to request clarification or add additional context in comments.

8 Comments

Looking a little closer right now. Do this implementation required the correct filename of unwanted files or just a substring? I tested and it seems that required all the correct filename
Any chance we can modify this to be a substring of the correct filename?
l.split('\\')[-1] in my answer is the substring of the correct filename. see my edit
@dudas: If you need that full substring detection capability, you should say so in the question! To do that requires a different strategy than Michael9's solution: it needs a double loop.
@dudas: In that case, maybe you should ask a new question, with appropriate example input.
|
1

you also could use a filter function with lambda

print filter(lambda x: x.split('\\')[-1] not in unwanted_files, list_dirs)
#['C:\\bar\\foo\\.world.txt']

or if you don't mind to import os (imo this is cleaner then splitting the string)

print filter(lambda x: os.path.basename(x) not in unwanted_files, list_dirs)

In a list comprehension it would look like this

[l for l in list_dirs if os.path.basename(l) not in unwanted_files]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.