0

I have a list like this :

list1 = ["a:b","x:y","s:e","w:x"]

I want convert into dictionary like this:

dict = {'a':'b', 'x':'y','s':'e','w':'x'}

The list is dynamic. How could i achieve this ?

Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
user2845399
  • 101
  • 2
  • 10
  • possible duplicate of [How to convert this list into dictionary in Python?](http://stackoverflow.com/questions/17144889/how-to-convert-this-list-into-dictionary-in-python) – Blue Ice Mar 13 '14 at 05:24

2 Answers2

2

You could do

>>> list1 = ["a:b","x:y","s:e","w:x"]
>>> dict(elem.split(':') for elem in list1)
{'a': 'b', 'x': 'y', 's': 'e', 'w': 'x'}
Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
1

Like this?

super_dict = dict()
for el in list1:
  super_dict[el[0]] = el[-1]

Of course there could be problems if the keys are identical, you'll need to add code if needed

Claudiordgz
  • 3,023
  • 1
  • 21
  • 48