In python i am getting a list in a variable like :
[ {'store_id': '321', 'first_name': 'A', 'name': 'A'},
{'second_id': '322', 'first_name': 'B', 'name': 'B', },
{'second_id': '323', 'second_name': 'c', 'name': 'c', },
{'second_id': '324', 'second_name': 'A', 'name': 'A', },
]
what i actually want is i want a list without duplicating the name . if it occur once then i want to remove t and create a new list with distinct data.i am stuck here i want all data in new list. how can i remove the duplicate data from the list .
in my case i want a ne list like :
Either
{'second_id': '322', 'first_name': 'B', 'name': 'B', },
{'second_id': '323', 'second_name': 'c', 'name': 'c', },
{'second_id': '324', 'second_name': 'A', 'name': 'A', },
]
Or
[ {'store_id': '321', 'first_name': 'A', 'name': 'A'},
{'second_id': '322', 'first_name': 'B', 'name': 'B', },
{'second_id': '323', 'second_name': 'c', 'name': 'c', },
]
And the code after that i am getting this is given below:
result = {}
data = request.POST
teamName = []
First = Test.objects.filter(d=data.get('id')).values(
'first_id','first_name').annotate(id=F('first_id'),name=F('first_name')).distinct()
Second = Test.objects.filter(id=data.get('id')).values(
'second_id','second_name').annotate(id=F('second_id'),name=F('second_name')).distinct()
combined_results = list(chain(First, Second))
for team in combined_results:
team['text'] = team['name']
team['id'] = team['id']
teamName.append(team)
if not combined_results:
result['status'] = False
result['data'] = ['Data not found']
else:
result['status'] = True
result['data'] = teamName
return JsonResponse(result)