1

I have a list like below

['position_Matrix position1', 'postal_code post10']

I was trying to make a dictionary based on this list and as expected below

{position_Matrix:position1, postal_code:post10}

Tried with below code and it doesn't work. Can someone help on this?

dict(map(lambda x: x.split(" "), alias.split(",")))
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
Gokul G
  • 15
  • 2

2 Answers2

5

Assuming alias is your list, the issue is that you're trying to split a list. You almost had it right, just remove the split. This will work:

dict(map(lambda x: x.split(" "), alias))
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
2

You could also use a dict comprehension:

d = {x.split()[0]: x.split()[1] for x in some_list}
#equivalent and more efficient
d = dict(map(str.split, some_list)) # this would be my preferred solution
#another equivalent (ugly)
d = dict(zip(*[y.split() for y in x]))
Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114