I want to create a list that will contain the name of the item and the prices for it from different lists. The method of adding to an existing name is ready(output is below). But if name does not exist in list1, name and price should be added separately. How can i do this?
How it looks now: [{'fullName': '★ StatTrak™ Karambit | Gamma Doppler Emerald (Minimal Wear)', 'price_steam': 9801.36, 'price_buff': 2000}, {'fullName': '★ Specialist Gloves | Tiger Strike (Factory New)', 'price_steam': 9797.64, 'price_buff': 1000}]
How I want: [{'fullName': '★ StatTrak™ Karambit | Gamma Doppler Emerald (Minimal Wear)', 'price_steam': 9801.36, 'price_buff': 2000}, {'fullName': '★ Specialist Gloves | Tiger Strike (Factory New)', 'price_steam': 9797.64, 'price_buff': 1000}, {'fullName': '★ StatTrak™ Karambit | Gamma Doppler Emerald (Factory New)', 'price_buff': 3000}]
list1 = [
{
"Name": "★ StatTrak™ Karambit | Gamma Doppler Emerald (Minimal Wear)",
"price_steam": 9801.36
},
{
"fullName": "★ Specialist Gloves | Tiger Strike (Factory New)",
"price_steam": 9797.64
}
]
list2 = [
{
"fullName": "★ StatTrak™ Karambit | Gamma Doppler Emerald (Minimal Wear)",
"price_buff": 2000
},
{
"fullName": "★ Specialist Gloves | Tiger Strike (Factory New)",
"price_buff": 1000
},
{
"fullName": "★ StatTrak™ Karambit | Gamma Doppler Emerald (Factory New)",
"price_buff": 3000
}
]
for elements1 in list1:
for elements2 in list2:
for key, value, in elements1.items() and elements2.items():
if key in elements1 and key in elements2 and elements1[key] == elements2[key]:
elements1.update(elements2)
if key in elements1 and key in elements2 and elements1[key] != elements2[key]:
#adding elements from list 2 to list1
CodePudding user response:
You can start the other way around and add the new element only if it was not found in list1
:
for product_to_add in list2:
found = False
for available_product in list1:
if product_to_add["fullName"] == available_product["fullName"]:
found = True
available_product.update(product_to_add)
if not found:
list1.append(product_to_add)
for item in list1:
print(item)
This will output your expected values:
{'fullName': '★ StatTrak™ Karambit | Gamma Doppler Emerald (Minimal Wear)', 'price_steam': 9801.36, 'price_buff': 2000}
{'fullName': '★ Specialist Gloves | Tiger Strike (Factory New)', 'price_steam': 9797.64, 'price_buff': 1000}
{'fullName': '★ StatTrak™ Karambit | Gamma Doppler Emerald (Factory New)', 'price_buff': 3000}
CodePudding user response:
Another approach is to use sets, assuming your lists does not have dicts with same "fullName".
names_list1 = set([d["fullName"] for d in list1])
names_list2 = set([d["fullName"] for d in list2])
# get names that are in list1 AND not in list2
diff_names = names_list2.difference(names_list1)
# viceversa
# diff_names = names_list1.difference(names_list2)
Now you can add your logic, you have the keys, just iterate and get the prices. This way is more efficient than iterate the two lists and compare the keys. Please keep in mind which list is going to be your "single source of truth", since it's important for the line where you get the difference btw sets