Inverse of dict(zip(x,y))

Tino Wildenhain tino at wildenhain.de
Fri Mar 6 08:27:05 EST 2009


Hi,

psykeedelik wrote:
> On Mar 5, 6:01 pm, Tino Wildenhain <t... at wildenhain.de> wrote:
>> Piet van Oostrum wrote:
>>>>>>>> Andre Engels <andreeng... at gmail.com> (AE) wrote:
>>>> AE> On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle <icym... at gmail.com> wrote:
>>>>>> Can someone suggest a easy method to do the inverse of dict(zip(x,y))
>>>>>> to get two lists x and y?
>>>>>> So, if x and y are two lists, it is easier to make a dictionary using
>>>>>> d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1,
>>>>>> x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...]
>>>>>> and y = [y1, y2, ...]
>>>> AE> x = d.keys()
>>>> AE> y = [d[e] for d in x]
>>>> AE> y = d.values() might also work, but I am not sure whether d.keys() and
>>>> AE> d.values() are guaranteed to use the same order.
>>> Yes, they are if the dictionary is not changed in the meantime (not even
>>> inserting and removing the same thing). See the library documentation,
>>> section dict.
>> Still I'd like to see an application where this really matters (that
>> keys() and values() match in order)
>>
>> Tino
>>
>>  smime.p7s
>> 4KViewDownload
> 
> First, thanks to all the guys who posted replies to my query!!
> 
> And then, just as an example to what Tino raised ...
> 
> I usually get properties that I compute, in a dictionary like property
> = [key1: val1, key2:val2, ...] and then I usually want to plot them in
> pylab, which AFAIK requires x and y as lists for the plot argument.
> Then I need to get the lists [key1, key2, ...] and [val1, val2, ...].
> And now I wonder if there a more efficient way doing what I described
> above!! ;)

I didn't check but I don't believe that. Usually x/y plots base on 
tuples for (x,y) and that's exactly what items() would deliver.

Regards
Tino
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20090306/fc550900/attachment-0001.bin>


More information about the Python-list mailing list