mercredi 8 avril 2015

Converting an array to hash in python


Vote count:

0




Trying to convert an list to dictionary but could no get expected output



p = {}
a = ["s","l","y"]
for s in a:
p["size"] = s

print(p)


output:



{'size': 'y'}


but i am expecting output like this



{'size': 's','size': 'l','size': 'y'}


how could i acheive this in python



asked 2 mins ago


1 Answer



Vote count:

0




You can't achieve your goal since you are using a dictionary. Pehaps you want to do this instead:



#! /usr/bin/python3

p = []
a = ["s","l","y"]
for s in a:
p += ("size", s)

print(p)


Output:



['size', 's', 'size', 'l', 'size', 'y']


answered just now

Controll

2,350





Converting an array to hash in python

Aucun commentaire:

Enregistrer un commentaire