in python i try to add another values for an existing dict. There is my existing dict :
d = {'132': '5ff446ee8', '133': '5ff446ef8871e234'}
I want add for each key a value, to get this :
d = {'132': ['5ff446ee8',"new_value"], '133': ['5ff446ef8871e234',"new_value2"]}
Sorry for my bad english, i have no idea how to do this ! If you can help my, i will be greatful
CodePudding user response:
I think this code will help you.
d = {'132': '5ff446ee8', '133': '5ff446ef8871e234'}
d["132"] = [d["132"], "new_value"]
print(d)
#output : {'132': ['5ff446ee8', 'new_value'], '133': '5ff446ef8871e234'}
You can perform this operation in a for loop on all keys of the dict
like :
d = {'132': '5ff446ee8', '133': '5ff446ef8871e234'}
new_dict = {}
for key, value in d.items():
new_dict[key]=[value, "new_value"]
print(new_dict)
#output : {'132': ['5ff446ee8', 'new_value'], '133': ['5ff446ef8871e234', 'new_value']}
Sorry for my bad english =)
CodePudding user response:
You can use Dict Comprehensions
and enumerate
and for creating a list use f-string
for new_value
.
>>> {k: [v, f'new_value_{idx}'] for idx, (k,v) in enumerate(d.items(), start=1)}
# If you want exactly your desired output try like below
>>> {k: [v, f'new_value{"" if idx==1 else idx}'] for idx, (k,v) in enumerate(d.items(), start=1)}
{'132': ['5ff446ee8', 'new_value1'], '133': ['5ff446ef8871e234', 'new_value2']}
{'132': ['5ff446ee8', 'new_value'], '133': ['5ff446ef8871e234', 'new_value2']}
CodePudding user response:
If you want to keep simple you can declare you dictionary values as an empty list and just append the new values.
d = {'132': [], '133': []}
d['132'].append('5ff446ee8')
d['133'].append('5ff446ef8871e234')
d['132'].append('new_value')
d['133'].append('new_value2')
print(d)
{'132': ['5ff446ee8', 'new_value'], '133': ['5ff446ef8871e234', 'new_value2']}
CodePudding user response:
You want more values to be added to existing keys in an existing dictionary, you could from the start make all the values as lists this way it'll be easier to add values whenever you wish, however here's my approach to achieve what you asked for:
def add_value(data, key, value):
if key in data.keys():
if type(data[key]) is list:
data[key] = data[key] [value]
else:
data[key] = [data[key]] [value]
else:
data[key] = value
return data
Here's an example:
d = {'132': '5ff446ee8', '133': '5ff446ef8871e234'}
add_value(d, '132', 'new_value')
print(d)
Output:
{'132': ['5ff446ee8', 'new_value'], '133': '5ff446ef8871e234'}
You could even implement this as a new class:
class NewDict(dict):
def __init__(self):
dict.__init__(self)
def add_value(self, key, value):
if key in self.keys():
if type(self[key]) is list:
self[key] = self[key] [value]
else:
self[key] = [self[key]] [value]
else:
self[key] = value
You could use it as follows:
d = {'132': '5ff446ee8', '133': '5ff446ef8871e234'}
new_d = NewDict()
new_d.update(d)
new_d.add_value('132', 'new_value')
print(new_d)
Output:
{'132': '5ff446ee8', '133': '5ff446ef8871e234', '123': 'new_value'}