Reputation: 65
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"
}
]
Upvotes: 0
Views: 191
Reputation: 38502
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
Upvotes: 1
Reputation: 566
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)
Upvotes: 1
Reputation: 120439
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'}]
Upvotes: 0
Reputation: 427
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())
Upvotes: 0