0
companies=['Python DS','PythonSoft','Pythzon','Pybook']
Data=[[12.87,13.23,11.42,13.10],[23.54,25.76,21.87,22.33],[98.99,102.54,97.21,100.065],[203.63,207.54,202.43,205.24]]
data_dict={}
for i in companies:
    for j in Data:
        data_dict[i]=j
print(data_dict)

Output:

{'Python DS': [203.63, 207.54, 202.43, 205.24], 'PythonSoft': [203.63, 207.54, 202.43, 205.24], 'Pythzon': [203.63, 207.54, 202.43, 205.24], 'Pybook': [203.63, 207.54, 202.43, 205.24]}

I want to link each element of companies to each element of Data and store as a list.

oreopot
  • 3,392
  • 2
  • 19
  • 28
  • If you’re actually using Python 3.0 (released in 2008), you should definitely upgrade. Otherwise, please specify the correct minor version or just tag [python-3.x] if unsure or if it’s unimportant. – Ry- May 10 '20 at 06:33
  • You can do `data_dict = dict(zip(companies, Data))` – modesitt May 10 '20 at 06:34
  • Don't nest your loop here. You need a single loop where you handle iterating over both lists at the same index. You would use the `zip` function for this typically. Indeed, you can just use the `dict` constructor this way: `data_dict = dict(zip(companies, Data))` – juanpa.arrivillaga May 10 '20 at 06:34
  • Ok. Thanks @juanpa.arrivillaga actually I am new to coding. Thanks for the answer. Will use zip function. – makkar_maverick May 10 '20 at 07:35

0 Answers0