Reputation: 391
I have a list as:
my_list = ["10", "12", "32", "23"]
and a dictionary as:
my_dict = {
'one': {'index': 0, 'sec_key': 'AB', 'id': '10'},
'two': {'index': 0, 'sec_key': 'CD', 'id': '12'},
'three': {'index': 0, 'sec_key': 'EF', 'id': '32'}
}
I want to keep a dictionary say final_dict which will have content of my_dict only if the id is present in my_list. I tried to do that by:
sf_dict = dict()
for list_id in my_list:
for key, value in my_dict.items():
if list_id == value['id']:
print("Values : {} {} {}".format(value['index'], value['sec_key'], value['id']))
sf_dict[key] = key
sf_dict[key]['index'] = value['index']
sf_dict[key]['sec_key'] = value['sec_key']
sf_dict[key]['id'] = value['id']
print(sf_dict)
I'm able to print the values but the assigning of those values is failing due to the error as:
TypeError: 'str' object does not support item assignment
Where am I making mistake?
Upvotes: 0
Views: 543
Reputation: 55
The already given answers should do the trick, altough maybe it is important to get why your approach isn't right.
The problem with what you did is that when you do sf_dict[key] = key
, you're setting the value of the dictionary for the key key
with the value key
. But key
is a string, thus, when you later try sf_dict[key]['index'] = value['index']
it tells you that you cannot assign on top a string.
Try to remove these lines
sf_dict[key]['index'] = value['index']
sf_dict[key]['sec_key'] = value['sec_key']
sf_dict[key]['id'] = value['id']
and simply replace them with sf_dict[key] = value
. This way, when you find the id you're searching for, you assign all the dictionary at once to the corresponding key.
Upvotes: 2
Reputation: 1329
#problem is here
sf_dict[key] = key
above line means sf_dict = {'one': 'one'} for example
so next lines mean 'one'['anything'] = so using index to assign into str is not allowed.
better solution is already answered above, just pointing issue in exist code
Upvotes: 1
Reputation: 33
for list_id in my_list:
for key, value in my_dict.items():
if list_id == value['id']:
print("Values : {} {} {}".format(value['index'], value['sec_key'], value['id']))
sf_dict[key] = {'index':value['index'],'sec_key':value['sec_key'],'id':value['id']}
print(sf_dict)
the code should be like this.
Upvotes: 1
Reputation: 10624
Here is one solution:
final_dict={}
for i in my_dict:
if my_dict[i]['id'] in my_list:
final_dict[i]=my_dict[i]
Example:
my_list = ["10", "32", "23"]
Output:
print(final_dict)
{'one': {'index': 0, 'sec_key': 'AB', 'id': '10'}, 'three': {'index': 0, 'sec_key': 'EF', 'id': '32'}}
Upvotes: 1
Reputation: 5746
You can use a dictionary comprehension to loop over your my_dict
and check if the value stored in id
is in your list.
my_list = ["10", "12", "32", "23"]
my_dict = {
'one': {'index': 0, 'sec_key': 'AB', 'id': '10'},
'two': {'index': 0, 'sec_key': 'CD', 'id': '12'},
'three': {'index': 0, 'sec_key': 'EF', 'id': '32'}
}
result = {key:value for key, value in my_dict.items() if value['id'] in my_list}
#{'one': {'index': 0, 'sec_key': 'AB', 'id': '10'}, 'two': {'index': 0, 'sec_key': 'CD', 'id': '12'}, 'three': {'index': 0, 'sec_key': 'EF', 'id': '32'}}
my_list = ["10"]
#{'one': {'index': 0, 'sec_key': 'AB', 'id': '10'}}
Upvotes: 5