One liner: creating a dictionary from list with indices as keys
a = [51,27,13,56]b = dict(enumerate(a))print(b)
will produce
{0: 51, 1: 27, 2: 13, 3: 56}
Return an enumerate object. sequence must be a sequence, an iterator, or some other object which supports iteration. The
next()
method of the iterator returned byenumerate()
returns atuple
containing a count (from start which defaults to 0) and the values obtained from iterating over sequence:
With another constructor, you have
a = [51,27,13,56] #given listd={i:x for i,x in enumerate(a)}print(d)
Try enumerate
: it will return a list (or iterator) of tuples (i, a[i])
, from which you can build a dict
:
a = [51,27,13,56] b = dict(enumerate(a))print b