Python Practice › Dictionary Programs › Program 30
Keep values that appear once in each list
Write a Python program to remove every value that is repeated inside each list value.
Input
{"marks1": [23, 28, 23, 69], "marks2": [25, 14, 25]}Output
{'marks1': [28, 69], 'marks2': [14]}Approach
- For each list keep items whose count is 1.
Solution
d = {"marks1": [23, 28, 23, 69], "marks2": [25, 14, 25]}print({k: [x for x in v if v.count(x) == 1] for k, v in d.items()})Output
{'marks1': [28, 69], 'marks2': [14]}Learn the concept
New to this topic? Read the lesson, then come back.