Use zip()
:
results, paths = zip(*((k, v) for k, v in lines.items() if keywrd in k))
as this'll produce the two lists in one step. The alternative is to use one for
loop:
results = []
paths = []
for (k,v) in lines.items():
if keywrd in k:
results.append(k)
paths.append(v)
List comprehensions are great if you want to build one list; if you need multiple from the same loop, just use the loop.
However, since this data comes from a SQLite query, your best bet would be to have SQLite limit the rows to those that match:
data.execute("select * from audio if filename LIKE ?", ('%{}%'.format(keywrd),))
Your lines
dictionary is far more efficiently built with a dictionary comprehension:
musics = data.execute("select * from audio")
lines = {row[1]: row[0] for row in musics}
or using a more specific query and a direct loop over the cursor:
data.execute("SELECT path, filename FROM audio WHERE filename LIKE ?",
('%{}%'.format(keywrd),))
paths, results = zip(*data)
LIKE
against a string with %
wildcards on both sides produces the same results an an in
test in Python; if keywrd
is contained in filename
the row matches.
Now there is no need to create an intermediary dictionary either.