Python: Add a Key-Value pair when a condition is true - python-3.x

Starting point are the following lists of dicts:
product = [
{'_id': '5678', 'variantIds':[{'id':'1'},{'id':'2'},{'id':'3'}]},
{'_id': '1234', 'variantIds':[{'id':'1'},{'id':'2'},{'id':'3'}]}
]
inventoryItem = [
{'_id': 'a6fdcf69', 'productId': '1234', 'variants': [{'variantId': '1', 'quantity': 0}, {'variantId': '2', 'quantity': 100}]},
{'_id': 'a6fdcf70', 'productId': '5678', 'variants': [{'variantId': '1', 'quantity': 0}, {'variantId': '2', 'quantity': 199}, {'variantId': '3', 'quantity': 299}]},
{'_id': 'a6fdcf77', 'productId': '9999', 'variants': [{'variantId': '1', 'quantity': 1111}, {'variantId': '2', 'quantity': 2222}, {'variantId': '3', 'quantity': 3333}]}
]
what i want is to add the key-value pair 'quantity':'value of quantity' to the first list of products. specifically i want to add it the the sub-list of dicts 'variantIds'. And I only want to add it when product[_'id] == inventoryItem['productId'] AND product['variantIds']['id] == inventoryItem['variants']['variantId'], so that e get the following output:
product = [
{'_id': '5678', 'variantIds':[{'id':'1', 'stockQuantity':0},{'id':'2', 'stockQuantity':199},{'id':'3', 'stockQuantity':299}]},
{'_id': '1234', 'variantIds':[{'id':'1', 'stockQuantity':0},{'id':'2', 'stockQuantity':100},{'id':'3', 'stockQuantity':0}]}
]
i can loop and add everything as long as the order in one list is corresponding to the order in the other list. but if it's not the case, what could be, i struggle with addressing the right index of the second list. how do you do that?
i think here is my closest try. but i already struggle on line 2, because i do not know the corresponding index in the inventoryItem List:
for i in product:
if i['_id'] == inventoryItem['productId']:
for j in i['variantIds']:
if j['id'] == inventoryItem['variants']['variantId']:
j['stock'] = inventoryItem['variants']['quantity']
print (product)

product = [
{'_id': '5678', 'variantIds':[{'id':'1'},{'id':'2'},{'id':'3'}]},
{'_id': '1234', 'variantIds':[{'id':'1'},{'id':'2'},{'id':'3'}]}
]
inventoryItem = [
{'_id': 'a6fdcf69', 'productId': '1234', 'variants': [{'variantId': '1', 'quantity': 0}, {'variantId': '2', 'quantity': 100}]},
{'_id': 'a6fdcf70', 'productId': '5678', 'variants': [{'variantId': '1', 'quantity': 0}, {'variantId': '2', 'quantity': 199}, {'variantId': '3', 'quantity': 299}]},
{'_id': 'a6fdcf77', 'productId': '9999', 'variants': [{'variantId': '1', 'quantity': 1111}, {'variantId': '2', 'quantity': 2222}, {'variantId': '3', 'quantity': 3333}]}
]
for prod in product:
try:
loc = [i['productId'] for i in inventoryItem].index(prod['_id'])
variantList = inventoryItem[loc]['variants']
for item in prod['variantIds']:
try:
subloc = [i['variantId'] for i in variantList].index(item['id'])
quantity = variantList[subloc]['quantity']
except:
quantity = 0
item.update({'stockQuantity':quantity})
except Exception as e:
pass
print(e.args) # you can remove this line, if this disturbs your output
print(product)
Now this may not be the best way to do it, but its pretty straightforward.
This is based on what I understood about the structure of the data. Maybe there is more to the data, and based on that it can be optimized further. But it works on the example set you provided.
What this does is, it grabs items (dictionaries) from product list. Searches for similar id in inventoryItem. Grabs that index (of the dictionary) inside inventoryItem. And then just scans to proper places in that entry of inventoryItem and then updates the product based on that.
Note : I have assumed here that there is no specific order to anything.
If there is an explanation necessary to any part, let me know.

The thing is that you mixed lists and dictionnary so you need to access to the lists of variantIds. You can do it as follow I think :
for i in range(len(product)):
for j in range(len(product[0]['variantIds'])):
product[i]['variantIds'][j]['stockQuantity'] = 0
print(product)
Output:
[{'_id': '5678', 'variantIds': [{'id': '1', 'stockQuantity': 0}, {'id': '2', 'stockQuantity': 0}, ...

Related

How to retrieve key value of dictionary with a specific key value, within a list of dictionaries

I would appreciate if someone would be kind enough to help me figure this out.
In this example I have a list of dictionaries where each dictionary contains various parameters about a stock 'Symbol'. I'm trying to retrieve the 'Quantity' of stocks in the list for a given 'Symbol'. If the 'Symbol' doesn't exist in the list it should return 0.
p = [
{'AveragePrice': '8.5334167347', 'AssetType': 'STOCK', 'Quantity': '124', 'Symbol': 'FNGU'},
{'AveragePrice': '6.5334167347', 'AssetType': 'STOCK', 'Quantity': '100', 'Symbol': 'SPY'},
{'AveragePrice': '7.5215053838', 'AssetType': 'STOCK', 'Quantity': '69', 'Symbol': 'LABU'}
]
def getposition(symbol):
for d in p:
if d["Symbol"] == symbol:
q = int(d["Quantity"])
else:
q = 0
return q
print("LABU", getposition("LABU"))
print("FNGU", getposition("FNGU"))
print("SPY", getposition("SPY"))
print("AAPL", getposition("AAPL"))
But I keep getting false results:
LABU 69
FNGU 0
AAPL 0
SPY 0
What would be the correct way to scan through the list to get the correct results?
Thanks!
You need to stop after finding the correct value, otherwise you always overwrite the found value if the required symbol is not in the last dict in the list (also, maybe raise an exception or use a better sentinel value, as a stock might actually be worth 0):
def getposition(symbol):
for d in p:
if d["Symbol"] == symbol:
return int(d["Quantity"])
return 0
However, this is O(n). Since we already assume symbol is unique, it would be better to use it as a key in a dict of dicts:
p = {
'FNGU': {
'AveragePrice': '8.5334167347',
'AssetType': 'STOCK',
'Quantity': '124',
'Symbol': 'FNGU'
},
'SPY': {
'AveragePrice': '6.5334167347',
'AssetType': 'STOCK',
'Quantity': '100',
'Symbol': 'SPY'
},
'LABU': {
'AveragePrice': '7.5215053838',
'AssetType': 'STOCK',
'Quantity': '69',
'Symbol': 'LABU'
}
}
Now we get direct O(1) access and don't need to iterate over the entire data:
print(int(p['SPY']['Quantity']))
100
For your function, your else statement caused the q to reset as it was in a for loop. By instead keeping the q outside of the scope of the for loop, setting it equal to 0 and then dropping the else statement, we are able to achieve the desired result:
p = [
{'AveragePrice': '8.5334167347', 'AssetType': 'STOCK', 'Quantity': '124', 'Symbol': 'FNGU'},
{'AveragePrice': '6.5334167347', 'AssetType': 'STOCK', 'Quantity': '100', 'Symbol': 'SPY'},
{'AveragePrice': '7.5215053838', 'AssetType': 'STOCK', 'Quantity': '69', 'Symbol': 'LABU'}
]
def getposition(symbol):
q = 0
for d in p:
if d['Symbol'] == symbol:
q = int(d["Quantity"])
return q
print("FNGU", getposition("FNGU"))
print("SPY", getposition("SPY"))
print("LABU", getposition("LABU"))
print("AAPL", getposition("AAPL"))
Hope this helps!

group dictionaries and get count

I have a list of dictionaries like this:
list1 = [{'name': 'maik','is_payed': 1, 'brand': 'HP', 'count': 1, 'items': [{'device': 'mouse', 'count': 110}]},{'name': 'milanie','is_payed': 0, 'brand': 'dell', 'count':10, 'items': [{'device': 'bales', 'count': 200}]}]
list2 = [{'name': 'maik','is_payed': 0, 'brand': 'HP', 'count': 20, 'items': [{'device': 'mouse', 'count': 1}]},{'name': 'nikola','is_payed': 1, 'brand': 'toshiba', 'count':10, 'items': [{'device': 'hard', 'count': 20}]}]
my_list= list1 + list2
count = pd.DataFrame(my_list).groupby(['name', 'is_payed'])
final_list_ = []
for commande, group in count:
print(commande)
records = group.to_dict("records")
final_list_.append({"name": commande[0],
"payed": commande[1],
"occurrence": len(group),
"items": pd.DataFrame(records).groupby('device').agg(
occurrence=('device', 'count')).reset_index().to_dict('records')})
I don't know how can I get it like this:
the 'payed' field is like this payed/total_commands
for example lets take maik he has two commands one is payed and the other one is not, so the final result will be like this:
{'name': 'maik','payed': 1/2, 'brand': 'HP', 'count': 21, 'items': [{'device': 'mouse', 'count': 111}]}
Since you just want to group by "name" and are only interested in the "played" values, let's concentrate on that and ignore the other data.
So for our purposes, your starting data looks like:
my_list = [
{'name': 'maik', 'is_payed': 1},
{'name': 'milanie', 'is_payed': 0},
{'name': 'maik', 'is_payed': 0},
{'name': 'nikola', 'is_payed': 1}
]
Now let's take a first pass over this data and count up the number of times we see a name and the number of times that name corresponds to an "is_payed" flag
results = {}
for item in my_list:
key = item["name"]
results.setdefault(key, {"sum": 0, "count": 0})
results[key]["count"] += 1
results[key]["is_payed"] += item["is_payed"]
At this point we have a dictionary that will look like:
{
'maik': {'is_payed': 1, 'count': 2},
'milanie': {'is_payed': 0, 'count': 1},
'nikola': {'is_payed': 1, 'count': 1}
}
Now we will take a pass over this dictionary and create our true final result:
results = [
{"name": key, "payed": f"{value['is_payed']}/{value['count']}"}
for key, value in results.items()
]
Giving us:
[
{'name': 'maik', 'payed': '1/2'},
{'name': 'milanie', 'payed': '0/1'},
{'name': 'nikola', 'payed': '1/1'}
]

Sort json list from another list

I'm trying to sort a json list from another list.
example:
jsonList = [{'id': 'das', 'name': 'something'}, {'id': 'rtn', 'name': 'Something Else'}, {'id': 'ddsn', 'name': 'Something ElseElse'}]
orderList = ['rtn', 'ddsn', 'das']
goodList = someFunction(jsonList, orderList )
I need the output to be the json list sorted by the id:
goodList = [{'id': 'rtn', 'name': 'Something Else'}, {'id': 'ddsn', 'name': 'Something ElseElse'}, {'id': 'das', 'name': 'something'}]
goodList = sorted(jsonList, key=lambda x: orderList.index(x['id']))
or if you want just sort by id
sorted(jsonList, key=lambda x : x['id'])

How to add new key in the existing dictionary and derive a nested dictionary from it in python?

Iam trying to add new key inside the existing dictionary to create a new nested dictionary
Below is the existing dictionary
I need to make a nested dictionary from the below dictionary
{'userId': 'thanks',
'jobTitleName': 'Program Directory',
'firstName': 'Tom', 'lastName': 'Hanks',
'preferredFullName': 'Tom Hanks',
'employeeCode': 'E3',
'region': 'CA',
'phoneNumber': '+00408-2222222',
'emailAddress': 'tomhanks#gmail.com',
'Full Name': 'TomHanks'}
This is what i tried:
key1=['userId','jobTitleName','firstName','lastName','employeeCode']
key2=['Full Name','phoneNumber','region','emailAddress']
jsonValue={
{'userId': 'thanks',
'jobTitleName': 'Program Directory',
'firstName': 'Tom', 'lastName': 'Hanks',
'preferredFullName': 'Tom Hanks',
'employeeCode': 'E3',
'region': 'CA',
'phoneNumber': '+00408-2222222',
'emailAddress': 'tomhanks#gmail.com',
'Full Name': 'TomHanks'}
}
empDetails={}
for k in key1:
empDetails[k]=jsonValue[k]
print("Key1", empDetails)
for k2 in key2:
empDetails['otherDetails'][k2]=jsonValue[k2]
But its not working
Expected:
Now i need to add new key as 'otherDetails' to derive a nested dictionary as follows
{'userId': 'thanks',
'jobTitleName': 'Program Directory',
'firstName': 'Tom', 'lastName': 'Hanks',
'preferredFullName': 'Tom Hanks',
'employeeCode': 'E3',
otherDetails{
'region': 'CA',
'phoneNumber': '+00408-2222222',
'emailAddress': 'tomhanks#gmail.com',
'Full Name': 'TomHanks'
}
}
Appreciate if anyone can give right solution?
Thanks
There are a couple of Problems in you code. First in your jsonValue you put a dict inside of a dict, but don't specify a key here. From context I assume you actually want to use an Array here (since you most likely have an array of employee data, If I'm wrong here just comment)
Then you try to assign to empDetails['otherDetails'][k2] however, you never initialize the dict in empDetails['otherDetails'] . because of this you actually try to assign to None (Because empDetails['otherDetails'][k2] will evaluate to None[k2]
key1 = ['userId', 'jobTitleName', 'firstName', 'lastName', 'employeeCode']
key2 = ['Full Name', 'phoneNumber', 'region', 'emailAddress']
jsonValue = [{
'userId': 'thanks',
'jobTitleName': 'Program Directory',
'firstName': 'Tom', 'lastName': 'Hanks',
'preferredFullName': 'Tom Hanks',
'employeeCode': 'E3',
'region': 'CA',
'phoneNumber': '+00408-2222222',
'emailAddress': 'tomhanks#gmail.com',
'Full Name': 'TomHanks'
}
]
for employee in jsonValue:
empDetails = {'otherDetails': {}}
for k in key1:
empDetails[k] = employee[k]
print("Key1", empDetails)
for k2 in key2:
empDetails['otherDetails'][k2] = employee[k2]
print("Key1", empDetails)
Filter out which keys you want to keep, then filter out the keys you want to move to the inner dict, then insert the inner dict.
from pprint import pprint
d = {
"userId": "thanks",
"jobTitleName": "Program Directory",
"firstName": "Tom",
"lastName": "Hanks",
"preferredFullName": "Tom Hanks",
"employeeCode": "E3",
"region": "CA",
"phoneNumber": "+00408-2222222",
"emailAddress": "tomhanks#gmail.com",
"Full Name": "TomHanks",
}
# Outer keys you want to keep
keys_to_keep = {'userId','jobTitleName','firstName','lastName','preferredFullName', 'employeeCode'}
# Keys you want to move into inner dict
keys_to_move = {'Full Name','phoneNumber','region','emailAddress'}
# Create dict to insert into
new_dict = {k: d[k] for k in keys_to_keep}
# Create dict to insert into above dict
insert_dict = {k: d[k] for k in keys_to_move}
# Insert inner dict
new_dict['otherDetails'] = insert_dict
pprint(new_dict)
Output:
{'employeeCode': 'E3',
'firstName': 'Tom',
'jobTitleName': 'Program Directory',
'lastName': 'Hanks',
'userId': 'thanks',
'preferredFullName': 'Tom Hanks',
'otherDetails': {'Full Name': 'TomHanks',
'emailAddress': 'tomhanks#gmail.com',
'phoneNumber': '+00408-2222222',
'region': 'CA'},
}

Below is a list of dictionary which I am trying to add in Excel

test = [
{
'product': 'Business 1',
'amount': '1000000',
'old_date': '01 Mar,2018 19:18:29',
'num': '2',
'status': 'review'
},
{
'product': 'Business 2',
'amount': '1000000',
'new_date': '01 Mar,2018 19:18:29',
'num': '92',
'status': 'progress'
}
]
How do I automate the process as the new_date and old_date keys keep changing based on data. It is basically a python list of dictionaries where out of 5 keys one key keeps changing. How do I dynamically add this to excel or CSV?

Resources