0

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)
Pankhuri
  • 25
  • 7
  • If you two different outputs then something is wrong – chess_lover_6 Mar 10 '21 at 03:51
  • @chess_lover_6 everythin is fine i am merging two queryset in a single list thats why i am getting this list that is fine . the thing is that i aslo want to remove duplicate through the loop – Pankhuri Mar 10 '21 at 03:53
  • Create an empty set (1) and an empty list (2). Traverse the original list. For each element, check whether the name is in set (1). If it is, continue to the next element. If it isn’t, append the element to list (2) and add the name to the set (1). – crcvd Mar 10 '21 at 03:55
  • Does this answer your question? [removing json items from array if value is duplicate python](https://stackoverflow.com/questions/36258240/removing-json-items-from-array-if-value-is-duplicate-python) – Skylar Liang Mar 10 '21 at 03:58
  • @Pankhuri instead of doing all this **normalize** your database schema. You will have to keep making weird workarounds unless you normalize your database schema. – Abdul Aziz Barkat Mar 10 '21 at 04:15
  • @AbdulAzizBarkat ur right but i cant do this now .that y i am here for a solution – Pankhuri Mar 10 '21 at 04:19

4 Answers4

3

This should give you the second form

names = set()
newList = []
for d in mylist:
    if d['name'] in names:
        continue
    else:
        newList.append(d)
        names.add(d['name'])

print(newList)

Output:

[{'store_id': '321', 'first_name': 'A', 'name': 'A'},
 {'second_id': '322', 'first_name': 'B', 'name': 'B'},
 {'second_id': '323', 'second_name': 'c', 'name': 'c'}]

EDIT:
If you want the first form, you will have to sort your original list in descending order of store_id/second_id using:

mylist = sorted(mylist, key=lambda x: x.get('store_id') or x.get('second_id'), reverse=True)

and then filter the list as earlier.

Shiva
  • 2,627
  • 21
  • 33
0

using list comprehension:

lst = [ {'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', },
]

uniqueLst = []
lst2 = []
[[lst2.append(x), uniqueLst.append(x['name'])] for x in lst if x['name'] not in uniqueLst]

print(lst2)
JacksonPro
  • 3,135
  • 2
  • 6
  • 29
0

I have tried to solve your issue, It may not be the best approach but it does the job.

lets suppose you have this list

orignal_list = [
        {'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', },
      ]

so we can make a function which can take original list and return a filtered list.

def filter_list(original_list):
    # temp list which will contains the filtered records 
    temp_list = list()

    # loop over original_list
    for original_item in original_list:
        # get values of dict expect id because every element have different id
        original_values = [*original_item.values()][1:]
        flag = True
        # loop over temp_list
        for temp_items in temp_list:
            # get values expect the id
            temp_values = [*temp_items.values()][1:]
            # check if sample/record/item already exists
            if original_values == temp_values:
                flag = False

        if flag:
            temp_list.append(original_item)

    return temp_list

I hope this will solve your problem I have tried to explain the code using comments, let me know if you did not understand any thing.

Usman Ghani Mughal
  • 613
  • 2
  • 7
  • 14
0

I did something similar with sets. Since A is a set of elements:

for i in A:
    if i not in B:
        B.append(i)
Chr_8580
  • 71
  • 5
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 27 '21 at 00:41