0

I'm quite new to Python and was wondering if there was a good way to create a new list of unduplicated users.

My problem is that I have something like this

[
{
    "userId": "987654321",
    "method": "CARD",
    "lastDigits": "1234",
    "type": "mc",
    "first_name": "Leroy",
    "last_name": "Jenkins",
    "exp": "01/23"
  },
  {
    "userId": "987654321",
    "method": "PAYPAL",
    "first_name": "Leroy",
    "last_name": "Jenkins"
  },
  {
    "userId": "123456789",
    "method": "CARD",
    "lastDigits": "4567",
    "type": "visa",
    "first_name": "Joe",
    "last_name": "Bloggs",
    "exp": "01/25"
  },
  {
    "userId": "46513498000",
    "method": "PAYPAL",
    "first_name": "Betty",
    "last_name": "White"
  }
]

Basically I need to match when the userId has matched and keep the object when "method": "CARD"instead of PAYPAL then reconstruct essentially the same list again but minus the duplicate userId's when the user has both CARD and PAYPAL

::EDIT:: User can just have PAYPAL. and if it does just have PAYPAL, just return that

example output needed

[
{
    "userId": "987654321",
    "method": "CARD",
    "lastDigits": "1234",
    "type": "mc",
    "first_name": "Leroy",
    "last_name": "Jenkins",
    "exp": "01/23"
  },
  {
    "userId": "123456789",
    "method": "CARD",
    "lastDigits": "4567",
    "type": "visa",
    "first_name": "Joe",
    "last_name": "Bloggs",
    "exp": "01/25"
  },
  {
    "userId": "46513498000",
    "method": "PAYPAL",
    "first_name": "Betty",
    "last_name": "White"
  }
]
3
  • 1
    Hi and welcome to SO. It is important for the community that you also demonstrate that you are working to solve your issue. The best way to do that in my opinion is to include the text based version of the source code you have so far, even if it is not working quite right. Commented Jan 20, 2022 at 14:31
  • Let me rephrase the requirement to see if I've got it right. If a userId has a method == 'CARD' you want that information but not if that same userId has both 'CARD' and 'PAYPAL' Commented Jan 20, 2022 at 14:32
  • 1
    If you want help getting started, reshape the list into a dictionary with a key of "userId" When you go to set a key with an entry of "PAYPAL" verify that this key has not be set already. Commented Jan 20, 2022 at 14:33

4 Answers 4

1

This will perfectly work for your. Try and check it

mylist=[
{
    "userId": "987654321",
    "method": "CARD",
    "lastDigits": "1234",
    "type": "mc",
    "first_name": "Leroy",
    "last_name": "Jenkins",
    "exp": "01/23"
  },
  {
    "userId": "987654321",
    "method": "PAYPAL",
    "first_name": "Leroy",
    "last_name": "Jenkins"
  },
  {
    "userId": "123456789",
    "method": "CARD",
    "lastDigits": "4567",
    "type": "visa",
    "first_name": "Joe",
    "last_name": "Bloggs",
    "exp": "01/25"
  },
  {
    "userId": "46513498000",
    "method": "PAYPAL",
    "first_name": "Betty",
    "last_name": "White"
  }
]
temp_list=[]
temp_id=[]
for x in mylist:
    if int(x['userId']) not in temp_id:
        temp_list.append(x)
        temp_id.append(int(x["userId"]))

print(temp_list)
Sign up to request clarification or add additional context in comments.

Comments

1

If I don't misunderstood your question then simple filtering will do the trick for you,

user_ids = []
final_users = []
for user in users:
    user_ids.append(int(user['userId']))
    if user['userId'] not in user_ids and user['method'] != 'PAYPAL':
        final_users.append(user)
print(final_users)

Working Code: https://rextester.com/COBGVU63922

3 Comments

Ye this is pretty much it, but i forgot to mention. The user can just have PAYPAL. i.e:
[ { "userId": "987654321", "method": "CARD", "lastDigits": "1234", "type": "mc", "first_name": "Leroy", "last_name": "Jenkins", "exp": "01/23" }, { "userId": "987654321", "method": "PAYPAL", "first_name": "Leroy", "last_name": "Jenkins" }, { "userId": "54982100", "method": "PAYPAL", "first_name": "Bilbo", "last_name": "Baggins" } ]
If this is the case, PAYPAL is returned
0
users = {}

for d in user_list:
    uid = d["userId"]
    # If user id not in users, we add it
    if uid not in users:
        users[uid] = d

    # Otherwise we check if the already recorded method was "PAYPAL", 
    # if so we overwrite it.
    elif users[uid]["method"] == "PAYPAL":
       users[uid] = d

# To convert dict we just created back to list:
user_list = list(users.values())
    

Comments

0

Use defaultdict:

from collections import defaultdict

newdata = defaultdict(dict)

for item in data:
    userid = newdata[item['userId']]
    if userid == {} and item['method'] == 'CARD':
        userid.update(item)

Output:

# newdata = list(newdata.values())
>>> newdata
[{'userId': '987654321',
  'method': 'CARD',
  'lastDigits': '1234',
  'type': 'mc',
  'first_name': 'Leroy',
  'last_name': 'Jenkins',
  'exp': '01/23'},
 {'userId': '123456789',
  'method': 'CARD',
  'lastDigits': '4567',
  'type': 'visa',
  'first_name': 'Joe',
  'last_name': 'Bloggs',
  'exp': '01/25'}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.