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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/data-structures-and-algorithms/python-data-structure-exercise-6.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics