Python Forum

Full Version: Code question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Is there a better way to write this piece of code? I'm looking for unique entries to append into a list.
l =[]
 
if mgr.get_obj_id(i).split('.')[:-1] not in l:
        l.append(mgr.get_obj_id(i).split('.')[:-1])
Thanks in advance.
Does it need to be a list? When I hear "unique", I think "put it in a set".

s = set()

# in your loop
s.add(mgr.get_obj_id(i).split('.')[:-1])

# all unique elements
print(s)