boctulus
boctulus

Reputation: 395

array_combine() from PHP equivalent in Python?

I'm trying to do something like:

keys = [2,3,10]  # or even with assosiative keys like "orange", "apple", ..
vals = [7,9,11]
dict = array_combine(keys,vals)

Any way to implent that example as using array_combine() bult-in funcion of PHP5 ?

https://www.php.net/manual/en/function.array-combine.php

Upvotes: 0

Views: 1176

Answers (1)

anon582847382
anon582847382

Reputation: 20371

dict(zip(keys, vals))

Seems to be what you want.

Demo:

>>> keys = [2,3,10]
>>> vals = [7,9,11]
>>> dict(zip(keys, vals))
{2: 7, 3: 9, 10: 11}

zip puts elements at corresponding indices within the two lists into tuples. We can then put those pairs together into a new structure with the dict constructor.

Upvotes: 4

Related Questions