Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am working on the data structures in using my jupyter notebook. When I am trying to use the list inside the Dictionary using the key, pair values. I am not able to achieve the result which I wanted. The results which I wanted are as follows:

{("math":80, 81, 20),("science":95,22,55),("english":75,90,99)}

The code which I am using is as shown below:

#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)

The above code is not giving me the desired results. Can anyone give me a solution for it?

1 Answer

0 votes
by (36.8k points)

In the below code the header indicates the key and score indicated the pair. I am using a function called slice_per and according to the length of the key that is 'header' we are arranging according to the value that is 'score'.

Note: The key pair is case sensitive so make sure with the spelling and case of the letters.

Code:

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)]

dict(zip(header, slice_per(score, len(header))))

Output:

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

Learn Python for Data Science Course to improve your technical knowledge.

Browse Categories

...