meaning of [ ]

Rustom Mody rustompmody at gmail.com
Sun Sep 3 23:36:12 EDT 2017


On Sunday, September 3, 2017 at 5:10:13 PM UTC+5:30, Rick Johnson wrote:
> Andrej Viktorovich wrote:
> > I suppose p becomes array of strings but what [] means in this statement?
> 
> Generally, it's an inline form of writing a loop that returns a list. There are other types as well.

Tsk tsk the confusioning continues

Rewrite
[p for p in sys.path] 
as
[p | p ∈ sys.path]

Is that clearer?

And then as

{p | p ∈ sys.path}
And refresh the idea of set-builder notation
http://www.mathwords.com/s/set_builder_notation.htm

Note the the clunky ascii-fication of (most) programming languages (including python)
is a minor distraction
The bigger and more real issue is that sets and lists are similar and different
Sets have no order, lists are ordered

As Peter pointed out this is a no-op
ie
[p for p in sys.path] 

could be written as
list(sys.path)

[Not sure why he didnt say just sys.path]

Anyway this is a good example to distinguish

[p for p in sys.path] 
from
{p for p in sys.path}

Both work in python
But the second is probably not correct because path-searching is order dependent



More information about the Python-list mailing list