perhaps some more experienced can explain me what’s wrong in my code. After several hours of investigating I’m giving up!
I’m getting from DB a list of objects (Investments). Some of the investments have the same “investment id”.
I want to build a dict with “investment id’s” as a keys and investments with the same “investment id” wrapped in list as a values
Here is a dummy code that emulate the investment class and build the list for test
class TestInvestment:
def __init__(self, id: str):
self.id = id
self.data = "bla...bla...bla"
def __repr__(self):
return f"Investment with code {self.id}"
test_investment1 = TestInvestment('RU000A100D89')
test_investment2 = TestInvestment('RU000A100YP2')
dummy_investments = [test_investment1, test_investment2]
and here is the function that actually build the dictionary. Getting all unique id’s as a set, than creating dict based on this set with empty list as values. On the end appending accordingly the investments to the dict values.
def combine_investments(investments: List[TestInvestment]):
unique_investments_codes = set([investment.id for investment in investments])
unique_investments_dict = dict.fromkeys(unique_investments_codes, [])
[unique_investments_dict[investment.id].append(investment) for investment in investments]
return unique_investments_dict
but on the end I’m getting strange result. All investments added to every key.
combined_investments = combine_investments(dummy_investments)
[print(item) for item in combined_investments.items()]
('RU000A100D89', [Investment with code RU000A100D89, Investment with code RU000A100YP2])
('RU000A100YP2', [Investment with code RU000A100D89, Investment with code RU000A100YP2])
I assume that the problem is somewhere in using the object attributes as a keys in dict. But not sure…
When I’m simply hardcoding the dict with exactly the same content, everything works perfectly!
def combine_investments(investments: List[TestInvestment]):
# unique_investments_codes = set([investment.id for investment in investments])
# unique_investments_dict = dict.fromkeys(unique_investments_codes, [])
unique_investments_dict = {'RU000A100D89': [], 'RU000A100YP2': []}
[unique_investments_dict[investment.id].append(investment) for investment in investments]
return unique_investments_dict
as a result I’m getting what I want!
('RU000A100D89', [Investment with code RU000A100D89])
('RU000A100YP2', [Investment with code RU000A100YP2])
Can some one guide me what is the reason for this behavior?
>Solution :
You’re creating the dictionary with dict.fromkeys(unique_investments_codes, []) – this creates a dictionary with those keys, where all the values are that same single list.
So, as you add to any of the values, you’re adding to all of them, as the value of each dictionary key is still just the same single list.