csv_text = """name,marks
Asha,91
Ravi,64
Meera,85
Dev,58"""
header, *lines = csv_text.splitlines()
keys = header.split(",")
rows = [dict(zip(keys, line.split(","))) for line in lines]
rows = [{**r, "marks": int(r["marks"])} for r in rows]
avg = sum(r["marks"] for r in rows) / len(rows)
print(f"Average: {avg}")
print([r["name"] for r in rows if r["marks"] > avg])
Output Verified by running it on Python 3
Average: 74.5
['Asha', 'Meera']
zip pairs headers with values; a second comprehension rebuilds each dict with marks converted to int; a generator expression computes the average.
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!