Reputation: 2384
I have following dict that I reuse a lot in my code:
data[day] = {
"time": 0,
"total": 0,
"date": day,
}
How can I make this dict a variable and reuse it. And set the day when by current day when running the dict.
Upvotes: 1
Views: 47
Reputation: 17322
DEFAULT = {
'time': 0,
'total': 0
}
def get_my_dict(day):
return {**DEFAULT, 'date': day}
use like:
data[day] = get_my_dict(day)
Upvotes: 0
Reputation: 61910
IIUC, you could use a function:
days = ['monday', 'tuesday']
def default(d):
return {"time": 0, "total": 0, "date": d}
data = {}
for day in days:
data[day] = default(day)
print(data)
Output
{'monday': {'time': 0, 'total': 0, 'date': 'monday'}, 'tuesday': {'time': 0, 'total': 0, 'date': 'tuesday'}}
Upvotes: 1