May-01-2020, 05:02 AM
If it were just a one-time thing for one object, I'd do the first one.
If it was happening more than once, I'd wonder why you're retaining the list if uniqueness is important.
If you needed to add several objects to the list uniquely, I'd do similar to #2, but use a set comprehension.
If it was happening more than once, I'd wonder why you're retaining the list if uniqueness is important.
If you needed to add several objects to the list uniquely, I'd do similar to #2, but use a set comprehension.
def add_objects_if_not_in_list(objs_to_add, in_list): return list({x for x in itertools.chain(in_list, objs_to_add)})The disadvantage of this and #2 from above is that the set would destroy any order of the original list. (which may be the only reason you would prefer to keep the list than the set)