Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello team, I have this python snippet whereby it contains 4 for loops which contain a dict and the output is repetitive kindly requesting for help for it to output only unique data. below is the snippet i will really appreciate your help. Thanks in Advance
for A_R_Tokens in Request_Token_List:
    for A_Account_No in V_Account_Payment_Phone_list[0]:
        for A_Payer_ID in V_PayerID_ProductID_list[0]:
            for A_Product_ID in V_PayerID_ProductID_list[1]:
                data = {
                '__RequestVerificationToken': A_R_Tokens,
                'PayerId-input': A_Account_No,
                'PayerId': A_Payer_ID,
                'ProductId': A_Product_ID,
                'QueueItemId': '',
                'Comment': ''
                }

On running it i get repetitive output
and on running the code this what i get
{'__RequestVerificationToken': 'se', 'PayerId-input': '31955948', 'PayerId': '1c17da55-a8bb-ea11-a9a9-', 'ProductId': '1e17da55-a8bb-ea11-a9a9-000d3a298bc6', 'QueueItemId': '', 'Comment': ''}
{'__RequestVerificationToken': 'se', 'PayerId-input': '31955948', 'PayerId': '1c17da55-a8bb-ea11-a9a9-', 'ProductId': 'dc0525fd-3bbe-e711-a952-000d3a26b1fe', 'QueueItemId': '', 'Comment': ''}
{'__RequestVerificationToken': 'se', 'PayerId-input': '31955948', 'PayerId': 'da0525fd-3bbe-e711-a952-000d3a26b1fe', 'ProductId': '1e17da55-a8bb-ea11-a9a9-000d3a298bc6', 'QueueItemId': '', 'Comment': ''}

and the output that i want is
{'__RequestVerificationToken': 'se', 'PayerId-input': '31955948', 'PayerId': '1c17da55-a8bb-ea11-a9a9-', 'ProductId': '1e17da55-a8bb-ea11-a9a9-000d3a298bc6', 'QueueItemId': '', 'Comment': ''}

I will really appreciate any help coming through

What I have tried:

I Have tried sets which didn't work because dictionaries can not be hash-able
Posted
Updated 4-Sep-20 3:26am

I don't understand the purpose of these 4 loops; especially if you are not going to use the variable from the previous loop as the list to iterate on.
for A_R_Tokens in Request_Token_List:
    for A_Account_No in V_Account_Payment_Phone_list[0]:
        for A_Payer_ID in V_PayerID_ProductID_list[0]:
            for A_Product_ID in V_PayerID_ProductID_list[1]:
Why even write the other three layers of the loop, when:

1. You are not going to iterate on each level, dependent of the previous layer?
1. You can easily access the elements using these indexers (e.g. V_Account_Payment_Phone_list[0]).

This direct indexing is the reason why in each loop you are getting the same (duplicated) results. Can you share the purpose behind the loop/algorithm?

Still, decrease the total number of loops here, the complexity of your program is O(N4) depending upon how many elements are in each list.
 
Share this answer
 
Thanks Ahmad i really appreciate your help, the problem is this lists i am iterating through aren't from the same function they are from different functions and this is the function whereby i need to iterate through the elements in the lists and post them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900