list to map?

Bruce Guenter bruce.guenter at qcc.sk.ca
Wed May 19 16:44:31 EDT 1999


On 18 May 1999 23:30:39 -0400, Alex <alex at somewhere.round.here> wrote:
>From the followups to followups I've seen (I always receive people's
>posts out of order), it looks like someone might already have said this.
>But in case not, you could use the operator module:
>
>>>> import operator
>>>> x = [(1,2),(3,4),(5,6)]
>>>> x_0 = map (operator.getitem, x, len (x) * [0])
>>>> x_1 = map (operator.getitem, x, len (x) * [1])
>>>> x_dictionary = {}
>>>> map (operator.setitem, len (x) * [x_dictionary], x_0, x_1)
>[None, None, None]
>>>> print x_dictionary
>{3: 4, 1: 2, 5: 6}

Many thanks.  This example showed me a different way of approaching the
same problem, since the original list of tuples is created from the x_0
and x_1 lists above.  I guess my Perl background shows here, since in
Perl, one can convert from an array directly into a hash.
-- 
Bruce Guenter, QCC Communications Corp.  EMail: bruce.guenter at qcc.sk.ca
WWW: http://www.qcc.sk.ca/~bguenter/
Go ahead!  SPAM me at bait at qcc.sk.ca




More information about the Python-list mailing list