SHENGNAN LI
SHENGNAN LI

Reputation: 73

Convert two lists to a dictionary?

def dbupdate(self):
    dicDccp = dict(zip(self.dccpdate, self.dccpValue))
    dicDcep = dict(zip(self.dcepdate, self.dcepValue))
    dicDccp = sorted(dicDccp.keys())
    dicDcep = sorted(dicDcep.keys())
    print(type(dicDccp))
    print(type(dicDcep))
    for (k1, v1), (k2, v2) in zip(dicDccp.items(), dicDcep.items()):
        self.Db.dbupdate(self.symbol, self.threshold, k1, v1, k2, v2)

I tried to convert the two lists to a dictionary. But the types of dicDccp and dicDcep were still lists.

enter image description here

Upvotes: 0

Views: 64

Answers (1)

tehforsch
tehforsch

Reputation: 316

The problem is the line dictDccp = sorted(dictDccp.keys())

sorted is a function that takes an iterable object (such as the keys() object that you give it) and returns a list. You are overwriting your dictDccp with this list.

If you were trying to iterate through the keys of the dictionaries in sorted order you could do

for (k1, v1), (k2, v2) in zip(sorted(dictDccp.items()), sorted(dictDcep.items())):
    self.Db.dbupdate(self.symbol, self.threshold, k1, v1, k2, v2)

Upvotes: 3

Related Questions