I have a CSV input file that I organized as a defaultdict:
{
'1234' : [('1,60', 'text', 'supplier1')],
'3456' : [('1,98', 'another text', 'supplier2')],
['1,54', 'text again', 'supplier1'],
'709' : [('2,90', 'again', 'supplier2')]
}
I would like to is prioritize tuples containing "supplier1". If there are results for the same key for supplier1 and 2, keep only supplier1. And if no results for a given key for supplier1, keeping results with other suppliers.
EDIT : desired output :
{
'1234' : [('1,60', 'text', 'supplier1')],
'3456' : ['1,54', 'text again', 'supplier1'],
'709' : [('2,90', 'again', 'supplier2')]
}
As defaultdict
is a subclass of dict
, we can assume a regular dictionary of lists (at the same time, I've fixed some syntax errors which I assume are typos):
d = {'1234' : [('1,60', 'text', 'supplier1')],
'3456' : [('1,98', 'another text', 'supplier2'),
('1,54', 'text again', 'supplier1')],
'709' : [('2,90', 'again', 'supplier2')]}
You can then use a dictionary comprehension with a custom function to perform your task:
def get_data(x):
for tup in x:
if tup[-1] == 'supplier1':
return [tup]
return x
res = {k: get_data(v) for k, v in d.items()}
{'1234': [('1,60', 'text', 'supplier1')],
'3456': [('1,54', 'text again', 'supplier1')],
'709': [('2,90', 'again', 'supplier2')]}