Home > Software engineering >  How to convert dict containing tuple values to string
How to convert dict containing tuple values to string

Time:04-28

How to convert the following Input to the desired output using python.

Input:

{'Id': (34,), 'user': ('[email protected]',), 'createdOn': ('12 Oct',), 'status': (False,), 'message': ('Hello',)}

Output:

{'Id': 34, 'user': '[email protected]', 'createdOn':'12 Oct', 'status': False, 'message': 'Hello'}

CodePudding user response:

Just take first index for each value :

your_dict = {'Id': (34,), 'user': ('[email protected]',), 'createdOn': ('12 Oct',), 'status': (False,), 'message': ('Hello',)}
your_dict = {key:val[0] for key, val in your_dict.items()}

CodePudding user response:

Why are the values in a tuple to begin with?

old_dict = {'Id': (34,), 'user': ('[email protected]',), 'createdOn': ('12 Oct',), 'status': (False,), 'message': ('Hello',)}
new_dict = {k:v[0] for k,v in old_dict.items()}
print(new_dict)
  • Related