Python Forum
Removing items from list if containing a substring
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing items from list if containing a substring
#1
I'm trying to identify a file within a directory by removing list items containing an unwanted substring. When the script finishes running, list2 only contains 'test.test1-test-sample.mp4' when it's expected to contain 'test.test-test.mp4' Any help with where my mistake is would be greatly appreciated.

File's in directory: test.test-test.mp4 (intended target file), test.test1-test-sample.mp4, test.test2-test-Sample.mp4

This is not the entire script, just the relevant part. Please ignore the irrelevant imports.
import tmdbsimple as tmdb
import requests
import locale
import os
import subprocess
import shlex
import json
tmdb.API_KEY = ''
api_key = ''


user_input = input("Enter a video file location: ")
dest = input("Enter a destination directory: ")

list1 = []
list2 = []
    
if os.path.isfile(user_input):
    list1.append(os.path.basename(user_input))
    base1 = os.path.splitext(list1[0])
    basename = base1[0]
else:
    list1.append(os.path.basename(user_input))
    basename = list1[0]
    for file in os.scandir(user_input):
        if file.name.endswith(('.mp4', '.mkv', '.avi')):
            list2.append(file.name)
    for j in list2:
        if "-sample" or "-Sample" in j:
            list2.remove(j)
    #target_file = os.path.join(user_input, list2[0])

#print(list1)                
print(list2)
#print(target_file)
Reply
#2
You have a couple of problems. Your most serious is the logic on line 29. By precedence, it will be parsed as the following:

        if ("-sample") or ("-Sample" in j):
As "-sample" is a string that is not empty, it is always true. This branch is always taken. If it's always taken, why is everything not removed?

Your second issue is trying to remove elements from a list while you're using it as an iterator. The order gets messed up.

This should remove 2, 3, and 4 from the list, but fails because the iterator is upset.
>>> l = list(range(6))
>>> for i in l:
...   if 2 <= i <= 4:
...     l.remove(i)
...
>>> print(l)
[0, 1, 3, 5]
While there are lots of ways around this (make a copy, use a comprehension to copy the bits you do want), an easier method here is to roll your exclusion into the previous loop. You don't have to remove something that you never stored in the first place.

    for file in os.scandir(user_input):
        if "-sample" in file.name or "-Sample" in file.name:
            continue
        if file.name.endswith(('.mp4', '.mkv', '.avi')):
            list2.append(file.name)
Reply
#3
Thanks for the thorough explanation. The if logic mistake makes sense. I'll read more into remove() so I understand this better. I also forgot continue was a thing. Ugh. Pressing on!

Your solution is simple and straightforward. Thanks again!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 80 Yesterday, 01:16 PM
Last Post: ann23fr
  extract substring from a string before a word !! evilcode1 3 491 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,257 May-22-2023, 10:39 PM
Last Post: ICanIBB
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,015 Apr-08-2023, 06:36 PM
Last Post: Winfried
  Finding combinations of list of items (30 or so) LynnS 1 836 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  ValueError: substring not found nby2001 4 7,846 Aug-08-2022, 11:16 AM
Last Post: rob101
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,497 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  Match substring using regex Pavel_47 6 1,370 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  How to get list of exactly 10 items? Mark17 1 2,403 May-26-2022, 01:37 PM
Last Post: Mark17
  how to assign items from a list to a dictionary CompleteNewb 3 1,535 Mar-19-2022, 01:25 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020