Python Remove Item from list -
i'm new python, working on it, guy in team left , need work on line, apologies if stupid question.
i need remove items list (they'll contain phrase) , have googled , tried few ways doesn't seem working.
what i've got is,
def get_and_count_card_files(date): # retrieve dc , sd card files dc_files = dc_card_files.get_dc_files(dc_start_location, date) sd_files = sd_card_files.get_sd_files(sd_start_location) print(dc_files) if '*nocover*' in dc_files: dc_files.remove('*nocover*')
dc_files being list (bunch of file names) , need remove has nocover in file name. exist print function shows me does.
anyone idea i'm doing wrong
with piece of code, *
won't glob or regular expression in these contexts:
if '*nocover*' in dc_files: dc_files.remove('*nocover*')
so better say:
if 'nocover' in dc_files:
however, have find right entry remove in list, without using wildcards.
for small list, iterate on it. list comprehension do:
new_dc_files = [dc dc in dc_files if 'nocover' not in dc]
it's compact way of saying:
new_dc_files = list() dc in dc_files: if 'nocover' in dc: continue new_dc_files.append(dc)
Comments
Post a Comment