in Education by
The data are kept in a list whose elements are dictionaries, where each dictionary contains the data for a single student: the student's ID number, the subject's name, as well as the number of points that he/she gained on each of the two partial exams, respectively. The format of each dictionary is the following: {'ID' : _IDnumber_, 'subject' : _'Artificial Intelligence'_, 'Partial Exam 1' : _points1_, 'Partial Exam 2' : _points2_} Now I need to define a function sum_partials(), which receives a single argument – list of dictionaries containing student data (as described above), and returns the same list, but modified in such a way that each dictionary will contain only the total score (i.e. the sum of points) of the partial exams instead of the scores for the two partial exams. Ex. result: [{'ID': 12217, 'subject': 'Artificial Intelligence', 'Total score': 55}, {'ID': 13022, 'subject': 'Artificial Intelligence', 'Total score': 85}, {'ID': 13032, 'subject': 'Artificial Intelligence', 'Total score': 47}] I have done it by using a function for editing each student which function I call as expression on a list comprehension: def sum_partials(results): # your code here def update_student(student): partial_exam1 = student['Partial Exam 1'] partial_exam2 = student['Partial Exam 2'] student.pop('Partial Exam 1') student.pop('Partial Exam 2') student['Total score'] = partial_exam1 + partial_exam2 return student return [update_student(student) for student in results] It works perfect, but I'm new to Python and I wonder if I can refactor my code!? Is there a solution for doing this in one row using only list comprehensions or maybe nested list comprehensions? I mean, to do all the stuff I need to do without the update_student() function but only by usinglist comprehensions ? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Keep in mind that while list comprehensions work, you might want to prioritize readable code rather than "this type of structure just because". Here, a simple for loop going through your list of students is just fine. def sum_partials(list_of_students): for student in list_of_students: student['Total score'] = student.pop('Partial Exam 1') + student.pop('Partial Exam 2') return list_of_students Thanks @BoarGules for the compact one-liner calculation with pop.

Related questions

0 votes
    I have list1 of a certain length and list2 of a certain length. Let's say: list1 = [32, 345, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    HOW DO I SORT THIS BY BIRTHYEAR IN ASCENDING ORDER? I'm distance learning student and have no help. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    Can the Python list comprehension syntax be used to create dictionaries? For an example: By iterating over pairs of ... #doesn't work Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How can I return the list in alphabets? I have sequence translator, and a python code that reads both ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I am trying to alter the data in the panda's df. Using below, where X >=5, I want to change the corresponding Y row to 1. Where X...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I think this is a common requirement eg. "How can I create an ACL like only author can edit his ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    List any four tools present in the Format tab to edit a picture. Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    In python3 console, input those: >>> import sys >>> sys.version_info sys.version_info(major=3, minor= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    In python3 console, input those: >>> import sys >>> sys.version_info sys.version_info(major=3, minor= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    In python3 console, input those: >>> import sys >>> sys.version_info sys.version_info(major=3, minor= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    In Python 2.7, I could get dictionary keys, values, or items as a list: >>> newdict = {1:0, 2:0, 3:0} ... to return a list in Python 3? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I want each item to be sorted by a specific property value from a list of dictionaries I have. Take the ... When sorted by name Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    By using Edit mode,how will you make modification in cell contents Select the correct answer from above options...
asked Nov 28, 2021 in Education by JackTerrance
0 votes
    I'm having trouble with understanding how to properly do unwrapping in Swift. Here is the situation: I have ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    What type of authentication attackers can detect via manual means and exploit them, using automated tools with password lists and dictionary attacks?...
asked Mar 20, 2021 in Technology by JackTerrance
...