Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Dictionary from a List of 2-Tuples

I have a list of 2-tuples like this:

l = [('a', 1), ('b', 2)]

and I want to be able to map this onto a dictionary object, so that I can do something like

l.a #=> 1

So I tried this, but why does it fail?

d = reduce(lambda y,x : y.update({x[0]:x[1]}),l,{})

This gives the error:

AttributeError: 'NoneType' object has no attribute 'update'

What am I doing wrong?

like image 996
JWL Avatar asked May 20 '11 13:05

JWL


1 Answers

>>> l = [('a', 1), ('b', 2)]
>>> d = dict(l)
>>> d['a']
1 
like image 114
Andrey Sboev Avatar answered Oct 11 '22 10:10

Andrey Sboev