Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code question
#1
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.
Larz60+ write Nov-22-2020, 10:45 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed this time. Please use code tags on future posts.
Reply
#2
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)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020