0

I have 2 different list. I want make them as a dict. But there is some prblem.As you see from code its key,value in list.How to make it to a dict ealy?

I make key,value in list and idk how to convert to real dict one. I share code for list one thanks a lot

I tried from dict but dict has no append. Imagine like a csv file. Header is column (8 column) , data is row (585 row)

for i in range(0,len(self.data)):
    for j in range(0,len(self.header)):
        self.output.append(self.header[j]+':'+self.data[i][j])

1 Answers1

0

It appears that you want to use zip to build a dict from two lists:

>>> header = 'jan feb mar'.split()
>>> data = [1, 2, 3]
>>> list(zip(header, data))
[('jan', 1), ('feb', 2), ('mar', 3)]
>>> dict(zip(header, data))
{'jan': 1, 'feb': 2, 'mar': 3}

EDIT

You updated the question to mention a CSV. To produce a dict d like that you will need to uniqueify with a row number i.

d = {f'{i:03d}-{hdr}': item
     for i, row in enumerate(data)
         for hdr, item in zip(header, row)}
J_H
  • 17,926
  • 4
  • 24
  • 44
  • here the biggest problem as you see from code header is duplicate. Len of header is 8 , len of data over 500. Clearly ; Each data has 8 column and header is name of this column. But I dont know how to make like that. I tried with stupid idea like header=header*len(data) but dict(zip(header,data)) doesnt count duplicate I guess –  Apr 15 '19 at 07:29