[Tutor] how i can change two lists into one directory
Steven D'Aprano
steve at pearwood.info
Fri Jul 23 02:37:44 CEST 2010
On Fri, 23 Jul 2010 09:51:37 am ANKUR AGGARWAL wrote:
> hey i have just started making a app using python and just gt a
> problem..
>
> i have two list
> a=["x","z"]
> b=[1,2]
>
> i want to make a directory like this
> c={"x":1,"z":2}
The word you want is "dictionary" or "dict", not directory.
Here's one slow, boring, manual way:
a = ["x", "z"]
b = [1, 2]
c = {}
c[a[0]] = b[0]
c[a[1]] = b[1]
You can turn that into a loop:
c = {}
for index in range( min(len(a), len(b)) ):
c[a[i]] = b[i]
Here's the sensible way that makes Python do all the heavy lifting:
c = dict(zip(a, b))
--
Steven D'Aprano
More information about the Tutor
mailing list