my_dict = {
"first_name": "Michael",
"last_name": "Mike",
"birthdate": "29.06.1980", # ***
"hobbies": ["Sing", "Compose", "Act"]
}
"birthdate"
is the key and the date is the value. I need to cast the date to a tuple inside the dictionary so it will be like this:
"birth_date": (29,06,1980)
CodePudding user response:
You can access the value corresponding to the 'birthdate'
key, then split
that string on '.'
, then convert each value to int
within a generator expression
>>> tuple(int(i) for i in my_dict['birthdate'].split('.'))
(29, 6, 1980)
If you want to replace the value in the dict then you can assign this value back to that original key.
>>> my_dict['birthdate'] = tuple(int(i) for i in my_dict['birthdate'].split('.'))
>>> my_dict
{'first_name': 'Michael', 'last_name': 'Mike', 'birthdate': (29, 6, 1980), 'hobbies': ['Sing', 'Compose', 'Act']}