Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a function for mapping each elem of an iter. seq. to corresponding ones of another?

I know that dict.fromkeys(a, b) takes an iterable sequence of keys (a), but only takes one (optional) value (None by default). I also know that update({k1: v1, k2: v2...}) takes either a dictionary, or something like a list of (key, value) tuples. But I have been unable to find a method that looks something like:-

new_dict = dict.method(lst1, lst2)
that maps each element of lst1 to corresponding element of lst2.

I am aware that I could use the map() function, with, say, a lambda as an argument or something similar, but I was looking for a direct (more efficient?) replacement.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks!

>Solution :

Use zip to zip the lists together into (key, value) tuples, and then pass the result to dict().

>>> lst1 = ['foo', 'bar']
>>> lst2 = [42, True]
>>> dict(zip(lst1, lst2))
{'foo': 42, 'bar': True}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading