I want unique elements in hubcode_list
. I can do it by
hubcode_alarm_obj = HubAlarm.objects.all()
for obj in hubcode_alarm_obj:
hubcode = obj.hubcode
if hubcode not in hubcode_list:
hubcode_list.append(hubcode)
I want to use list comprehension. I'm trying this but it says hubcode_list
is undefined, which is the correct bug. How can I only add unique elements in hubcode_list
hubcode_alarm_obj = HubAlarm.objects.all()
hubcode_list = [obj.hubcode for obj in hubcode_alarm_obj if obj.hubcode not in hubcode_list]
I can also do it by:-
hubcode_list = list(set([obj.hubcode for obj in hubcode_alarm_obj]))
But set is again another operation. Can I do it by using if-statement
in list-comprehension?