0
my_lst = ['b','c','a']
dict = {}
for i in my_lst[]:
    dict[i] = []
print dict
# prints: => {'c': [], 'b': [], 'a': []}

How can i get dict to be {'b': [ ], 'c': [ ], 'a': [ ]} with the keys in a similar order as in the list?

mdml
  • 22,442
  • 8
  • 58
  • 66
UdaraW
  • 98
  • 1
  • 8

1 Answers1

3

You're looking for an OrderedDict:

>>> from collections import OrderedDict
>>> my_lst = ['b','c','a']
>>> D = OrderedDict()
>>> for i in my_lst:
...     D[i] = []
>>> D
OrderedDict([('b', []), ('c', []), ('a', [])])
>>> print D.items()
[('b', []), ('c', []), ('a', [])]
mdml
  • 22,442
  • 8
  • 58
  • 66