joey
joey

Reputation: 115

how to extract a dictionary from a data structure using python?

so here is the table, named grade_lists, that maps names of students to lists of their exam grades. The grades should be converted from strings to integers. For instance, grade_lists['Thorny'] == [100, 90, 80]. can someone help me?

 grades = [ #here is the table
    ['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
    ['Thorny', '100', '90', '80'],
    ['Mac', '88', '99', '111'],
    ['Farva', '45', '56', '67'],
    ['Rabbit', '59', '61', '67'],
    ['Ursula', '73', '79', '83'],
    ['Foster', '89', '97', '101']
]

Upvotes: 0

Views: 1424

Answers (5)

user8477049
user8477049

Reputation:

Instead of iterating though list and then converting it to dictionary, you can directly use dictionary comprehension. It's more Pythonic.

grades = [ #here is the table
    ['Student', 'Exam_1', 'Exam_2', 'Exam_3'],
    ['Thorny', '100', '90', '80'],
    ['Mac', '88', '99', '111'],
    ['Farva', '45', '56', '67'],
    ['Rabbit', '59', '61', '67'],
    ['Ursula', '73', '79', '83'],
    ['Foster', '89', '97', '101']
]
grade = {g[0]:g[1:] for g in grades[1:]}
print(grade)

# Output
#{'Thorny': ['100', '90', '80'], 'Mac': ['88', '99', '111'], 'Ursula': ['73', '79', '83'], 'Farva': ['45', '56', '67'], 'Foster': ['89', '97', '101'], 'Rabbit': ['59', '61', '67']}

Upvotes: 0

Pardhu
Pardhu

Reputation: 1957

Simple one liner for the above problem:

grades_dict = dict(map(lambda x: (x[0], x[1:]), grades))

Upvotes: 0

Austin
Austin

Reputation: 26037

Use a dictionary-comprehension that maps values to integer:

{x[0]: list(map(int, x[1:])) for x in grades[1:]}

Example:

grades = [ #here is the table
    ['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
    ['Thorny', '100', '90', '80'],
    ['Mac', '88', '99', '111'],
    ['Farva', '45', '56', '67'],
    ['Rabbit', '59', '61', '67'],
    ['Ursula', '73', '79', '83'],
    ['Foster', '89', '97', '101']
]

print({x[0]: list(map(int, x[1:])) for x in grades[1:]})

# {'Thorny': [100, 90, 80], 
#  'Mac': [88, 99, 111],
#  'Farva': [45, 56, 67],
#  'Rabbit': [59, 61, 67],
#  'Ursula': [73, 79, 83],
#  'Foster': [89, 97, 101]}

This outputs a dictionary with name as key and list of marks each converted to integer as value of that key.

Upvotes: 2

Matthew Gaiser
Matthew Gaiser

Reputation: 4803

Just run this block of code.

grades = [ #here is the table
    ['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
    ['Thorny', '100', '90', '80'],
    ['Mac', '88', '99', '111'],
    ['Farva', '45', '56', '67'],
    ['Rabbit', '59', '61', '67'],
    ['Ursula', '73', '79', '83'],
    ['Foster', '89', '97', '101']
]
dict = {}
for student_data in grades:
   if student_data[0] == "Student":
     continue
   else:
     temp_list = []
     temp_list.append(int(student_data[1]))
     temp_list.append(int(student_data[2]))
     temp_list.append(int(student_data[3]))
     dict[student_data[0]] = temp_list

print(dict)

Upvotes: 0

Chris
Chris

Reputation: 29742

Just iterate through the list with dict:

grades_dict = dict((x[0],x[1:]) for x in grades[1:])
grades_dict

Output:

{'Farva': ['45', '56', '67'],
 'Foster': ['89', '97', '101'],
 'Mac': ['88', '99', '111'],
 'Rabbit': ['59', '61', '67'],
 'Thorny': ['100', '90', '80'],
 'Ursula': ['73', '79', '83']}

and grades_dict['Thorny']:

['100', '90', '80']

Upvotes: 5

Related Questions