0 votes
1 view
in Python by (43.4k points)

I'm having troubles in populating a python dictionary starting from another dictionary.

Let's assume that the "source" dictionary has a string as keys and has a list of custom objects per value.

I'm creating my target dictionary exactly as I have been creating my "source" dictionary how is it possible this is not working?

I get

TypeError: unhashable type: 'list'

Code :

aTargetDictionary = {} 

for aKey in aSourceDictionary: 

 aTargetDictionary[aKey] = [] 

 aTargetDictionary[aKey].extend(aSourceDictionary[aKey])

The error is on this line : aTargetDictionary[aKey] = []

1 Answer

0 votes
by (98.8k points)

The error you are getting because things don't add up. If aSourceDictionary is a dictionary, then your for loop has to work properly.

>>> source = {'a': [1, 2], 'b': [2, 3]} 

>>> target = {} 

>>>for key in source: 

... target[key] = [] 

... target[key].extend(source[key]) 

... 

>>> target 

{'a': [1, 2], 'b': [2, 3]} 

>>>

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...