Python Data Structure: Find the class wise roll number from a tuple-of-tuples
Python Data Structure: Exercise-6 with Solution
Write a Python program to find the class wise roll number from a tuple-of-tuples.
Sample Solution:-
Python Code:
from collections import defaultdict
classes = (
('V', 1),
('VI', 1),
('V', 2),
('VI', 2),
('VI', 3),
('VII', 1),
)
class_rollno = defaultdict(list)
for class_name, roll_id in classes:
class_rollno[class_name].append(roll_id)
print(class_rollno)
Sample Output:
defaultdict(<class 'list'>, {'V': [1, 2], 'VI': [1, 2, 3], 'VII': [1]})
Flowchart:

Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to count the most common words in a dictionary.
Next: Write a Python program to count the number of students of individual class.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics