Create set of list python

Hi,
I am trying to create a set out of the list that contains a names of the curve objects.
but I do not know where is my mistake, because its showing only one item of the list if the output is “a”, but when its print then I get each item as a separate list.



Here is the code:

import Rhino as r
import scriptcontext as sc
sc.doc=r.RhinoDoc.ActiveDoc
llist=
for i in x:
llist.append(r.RhinoDoc.ActiveDoc.Objects.Find(i).Attributes)
for i in range(len(llist)):
print set(llist[i].Name)
sc.doc=ghdoc

What I want to is basically, have each object’s name as an item in the list and create a set out of it.

Note:
Currently the name of the objects are integers, however when the names contains any chars then I get each char as an item of the list instead of each name as an item…

I appreciate any help,
Thanks in advance.

No need for a loop using range. Just use a list comprehension to get the list of names, then create a set from that.

a = set([i.Name for i in llist])
1 Like

Thank you very much for helping Nathan.

Or even neater, use a set comprehension

a = {i.Name for i in llist}
3 Likes

Did not know this existed. Nice work Graham.

Regards,
Terry.

1 Like

I did not know either, thanks a lot.

1 Like

Yes, there are also dictionary comprehensions:

a = {i.Name : i for i in llist}

where is that gold nugget of documentation we missed? :slight_smile:

Here it is :smiley: :snake:

https://docs.python.org/2.7/tutorial/datastructures.html#sets

1 Like

How could I forget about these other comprehensions. I use them a lot :open_mouth:

Go on, salvage your pythonic reputation by telling us about generator expressions and when to use them …

1 Like