'inputing a list of tuples into a dictionary - python programming
I'm taking a MOOC on introduction to python programming, and have no idea how to proceed with one of the problem sets on dictionaries. Could you please provide some guidance, so I can follow your code? I will still be able to learn from going through your code. Thank you.
The problem statement is: Write a function called course_info that takes as input a list of tuples. Each tuple contains two items: the first item in each tuple is a student's name as a string, and the second item in each tuple is that student's age as an integer.
The function should return a dictionary with two keys. The key "students" should have as its value a list of all the students (in other words, a list made from the first value of each tuple), in the original order in which they appeared in the list. The key "avg_age" should have as its value a float representing the average age of all the students in the list (in other words, the average of all the second items in the tuples).
For example:
course_info([("Jackie", 20), ("Marguerite", 21)]) -> {"students": ['Jackie', 'Marguerite'], "avg_age": 20.5}
Hint: Concentrate first on building the list of students and calculating the average age. Save creating the dictionary for last.
Write your function here!
Below are some lines of code that will test your function. You can change the value of the variable(s) to test your function with different inputs.
If your function works correctly, this will originally print (although the order of the keys may vary):
{'avg_age': 20.5, 'students': ['Jackie', 'Marguerite']}
print(course_info([("Jackie", 20), ("Marguerite", 21)]))
Solution 1:[1]
While I won't provide my solution code outright, I will walk you through my thought process in constructing it at a relatively high level:
First, I consider what the inputs and outputs of the program should be. In this case, those are the following:
Input: A list of tuples, where the first item in the tuple is the name and the second item in the tuple is an age.
Output: A dict with two K/V pairs, the first listing all of the student names and the second indicating the average age of the students.
Once this is done, I then devise my algorithm:
- Based on the output, I know that I need to involve all parts of all of the tuples, so a single
forloop iterating over each tuple should suffice. - I also know the K/V pairs in the output dict are each composed of different parts of each of the input tuples, so I know that on each loop iteration, I need to break apart the tuple so that I can start to form the output.
- I know that one part of the output dictionary is just a list of the student names, so I can take the first part of the current tuple and add it to a running list.
- I know that the second part of the output dictionary is an average of the ages, so I need a way to take the second part of the current tuple and add it to a running average.
- Once the loop is all said and done, I can combine everything together.
If you have questions about this, feel free to ask.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fireshadow52 |
