Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Merge Two Lists into a Dictionary

Python Merge Two Lists into a Dictionary

Document Object Reference Manual Python3 Examples

Suppose we have two lists, one containing keys and the other containing values. We can use Python's zip function to merge the two lists into a dictionary.

Example

keys

=

[

'name'

,

'age'

,

'city'

]

values

=

[

'Alice'

,

25

,

'New York'

]

Use the zip function to merge the two lists into a dictionary

my_dict

=

dict

(

zip

(

keys

,

values

)

)

print

(

my_dict

)

Code analysis:

Output result:

Example

{

'name'

:

'Alice'

,

'age'

:

25

,

'city'

:

'New York'

}

Document Object Reference Manual Python3 Examples