meaning of [ ]

Ben Bacarisse ben.usenet at bsb.me.uk
Mon Sep 4 09:05:51 EDT 2017


Rustom Mody <rustompmody at gmail.com> writes:

> 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

But [p for p in sys.path] is a list and "set-builder" notation is used
for sets.  Order is crucial for sys.path.  You say exactly that below so
I don't see how referring to sets helps anyone understand lists.

<snip>
> As Peter pointed out this is a no-op
> ie
> [p for p in sys.path] 
>
> could be written as
> list(sys.path)

Both make a copy -- that's not a no-op.  It may be a very-little-op but
not nothing.

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

Because he wanted code equivalent to [p for p in 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

Right.  So i'm puzzled why you suggest that [p for p in sys.path] should
be understood by reading about set-builder notation.

-- 
Ben.



More information about the Python-list mailing list