0

In the below string we have to get slipt the dict vals with the & and the keys are are LHS and their respective RHS are values in the dict.

Input

s = 'term=food&location=New York'

Expected Output

{term:'food', location:'New York'}

I've tried

a_dict = dict([s.strip('{}').split("&"),])
pairs=[item.split('=')for item in items]
d = dict(pairs)

Help me out

Thanks

krishna krish
  • 84
  • 1
  • 8

2 Answers2

1

Here's a short way of doing it

s = 'term=food&location=New York'
s = [item.split('=') for item in s.split('&')]

print(dict(s))

Outputs:

{'term': 'food', 'location': 'New York'}
Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
0
s = 'term=food&location=New York'
a_dict = s.split("&")
pairs=[item.split('=')for item in a_dict]   
d = dict(pairs)
Prabhat Mishra
  • 951
  • 2
  • 12
  • 33