0

I like to form a dictionary with two lists, header as the key and score as the values:

#data
header= ["math","science","english"]
score = [80,95,75,81,22,90,20,55,99]

#my attempt
d={}
for i in header:
    print(i)
    for j in score:
        print(j)

Desired output is

{("math":80, 81, 20),("science":95,22,55),("english":75,90,99)}
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Shan
  • 27
  • 1

6 Answers6

3
>>> {k: score[i::3] for i, k in enumerate(header)}
{'math': [80, 81, 20], 'science': [95, 22, 55], 'english': [75, 90, 99]}
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
1

We can make use of zip in this scenario.

groups = [score[i::3] for i in range(0, len(header))]

dict(zip(header, groups))
{'english': [75, 90, 99], 'math': [80, 81, 20], 'science': [95, 22, 55]}
gold_cy
  • 13,648
  • 3
  • 23
  • 45
1

Assuming you want your output to be a valid dictionary. Then try this.

dict(zip(header,zip(*[score[i:i+3] for i in range(0,len(score),3)])))
# {'math': (80, 81, 20), 'science': (95, 22, 55), 'english': (75, 90, 99)}
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
0

You have:

>>> header= ["math","science","english"]
>>> score = [80,95,75,81,22,90,20,55,99]

The first idea is to use zip, but zip stops when the shortest of the two lists is exhausted:

>>> dict(zip(header, score))
{'math': 80, 'science': 95, 'english': 75}

You have to use a second zip to group the scores:

>>> n = len(header)
>>> L = list(zip(*(score[i*n:(i+1)*n] for i in range(n))))
>>> L
[(80, 81, 20), (95, 22, 55), (75, 90, 99)]

And then to zip the header and the grouped scores:

>>> dict(zip(header, L))
{'math': (80, 81, 20), 'science': (95, 22, 55), 'english': (75, 90, 99)}
jferard
  • 7,835
  • 2
  • 22
  • 35
0

You should try to divide your problem into multiple smaller problems. Here are two links to related questions and their answers here on Stack Overflow:

  1. How to Split Python list every Nth element

    Choose one of the multiple possible solutions for a slice_per function there, so you'll get:

    >>> header = ["math", "science", "english"]
    >>> score = [80, 95, 75, 81, 22, 90, 20, 55, 99]
    >>> def slice_per(source, step):
    ...     return [source[i::step] for i in range(step)]
    ... 
    >>> slice_per(score, len(header))
    [[80, 81, 20], [95, 22, 55], [75, 90, 99]]
    
  2. Convert two lists into a dictionary

    Combine the slices with your headers:

    >>> dict(zip(header, slice_per(score, len(header))))
    {'math': [80, 81, 20], 'science': [95, 22, 55], 'english': [75, 90, 99]}
    
finefoot
  • 9,914
  • 7
  • 59
  • 102
0

you could use a dictionary comprehension and the buil-in function and enumerate:

step = len(header)
{k : score[i::step] for i, k in enumerate(header)}

output:

{'math': [80, 81, 20], 'science': [95, 22, 55], 'english': [75, 90, 99]}

or you can use numpy:

import numpy as np

dict(zip(header, np.array(score).reshape(3,3).T.tolist()))

output:

{'math': [80, 81, 20], 'science': [95, 22, 55], 'english': [75, 90, 99]}

if you want to use for loops:

result = {}
for i in range(len(score) // len(header)):
    for j, h in enumerate(header):
        result.setdefault(h, []).append(score[i * len(header) + j])

result

output:

{'math': [80, 81, 20], 'science': [95, 22, 55], 'english': [75, 90, 99]}
kederrac
  • 16,819
  • 6
  • 32
  • 55