-1
I have some dictionaries that I need to fill recursively and for that I would like to use list comprehensions this way
list_dict = [{'a':'b'},{'a':'c'}]
list_dict = [dict.update({'b':dict['a']}) for dict in list_dict]
The goal was to work with datetime where I would add a value to each dictionary.
The problem is that when using Dict.update() it returns None instead of the dictionary value which returns more or less this:
[None,None]
I also tried to use a map() in this way:
list(map(lambda dict: dict.update({'novo_valor':100}),list_dict))
But I got the same return.
I tried some other things like using and inside the list comprehension but without success.
Does anyone have any ideas that might help.
Practical example:
MAX_DAYS_TO_INACTIVE = 15
MAX_DAYS_TO_EXPIRATION = 30
Entrada = {
'UserName':'user.1',
'AccessKeyId':'CCCCCCCCCCCCCCCCCCCC',
'Status':'Inactive',
'CreateDate':datetime.datetime(2019,9,10,20,12,45,tzinfo=tzutc())
},
{
'UserName':'user.2',
'AccessKeyId':'BBBBBBBBBBBBBBBBBBBB',
'Status':'Active',
'CreateDate':datetime.datetime(2020,4,16,1,26,18,tzinfo=tzutc())
}
]
Function I created:
def add_date_paramters(complete_access_key_list):
key_list = list()
for acess_key in complete_access_key_list:
acess_key.update(
{
'InvalidationDate':acess_key['CreateDate']+datetime.timedelta(days=MAX_DAYS_TO_INACTIVE),
'ExpirationDate':acess_key['CreateDate']+datetime.timedelta(days=MAX_DAYS_TO_EXPIRATION),
}
)
key_list.append(acess_key)
return key_list
I hope I have something like this:
saida = [ {
'UserName':'user.1',
'AccessKeyId':'CCCCCCCCCCCCCCCCCCCC',
'Status':'Inactive',
'CreateDate':datetime.datetime(2019,9,10,20,12,45,tzinfo=tzutc()),
'InvalidationDate':datetime.datetime(2019, 9, 25, 20, 12, 45, tzinfo=tzutc()),
'ExpirationDate':datetime.datetime(2019, 10, 10, 20, 12, 45, tzinfo=tzutc())
},
{
'UserName':'user.2',
'AccessKeyId':'BBBBBBBBBBBBBBBBBBBB',
'Status':'Active',
'CreateDate':datetime.datetime(2020,4,16,1,26,18,tzinfo=tzutc()),
'InvalidationDate':datetime.datetime(2020, 5, 1, 1, 26, 18, tzinfo=tzutc()),
'ExpirationDate':datetime.datetime(2020, 5, 16, 1, 26, 18, tzinfo=tzutc())
}, ]
You have at least one practical example with an input and expected output ?
– Lacobus
sorry I could not answer everything here, completed in question
– Yuri Pastore Aranha
@Lacobus Thanks I managed to solve.
– Yuri Pastore Aranha