Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - iterating over result of list.append

Tags:

python

list

why can't I do something like this:

files = [file for file in ['default.txt'].append(sys.argv[1:]) if os.path.exists(file)]
like image 699
Martin Drlík Avatar asked Jul 01 '26 19:07

Martin Drlík


1 Answers

list.append doesn't return anything in Python:

>>> l = [1, 2, 3]
>>> k = l.append(5)
>>> k
>>> k is None
True

You may want this instead:

>>> k = [1, 2, 3] + [5]
>>> k
[1, 2, 3, 5]
>>> 

Or, in your code:

files = [file for file in ['default.txt'] + sys.argv[1:] if os.path.exists(file)]
like image 146
Eli Bendersky Avatar answered Jul 03 '26 10:07

Eli Bendersky