meaning of [ ]
Peter Otten
__peter__ at web.de
Sun Sep 3 07:34:20 EDT 2017
Andrej Viktorovich wrote:
> Hello,
>
> Trying to understand command:
> [p for p in sys.path]
>
> It prints array of paths. I suppose p becomes array of strings but what []
> means in this statement?
This is called "list comprehension", and
paths = [p for p in sys.path if "foo" in p]
is roughly equivalent to
paths = []
for p in sys.path:
if "foo" in p:
paths.append(p)
Your original example
> [p for p in sys.path]
just copies the items from sys.path into a new list. This is usually done
with the more concise
list(sys.path)
More information about the Python-list
mailing list