Keep values that appear once in each list

EasyDictionary Programs · 30 of 94

Write a Python program to remove every value that is repeated inside each list value.

Advertisement
Input
{"marks1": [23, 28, 23, 69], "marks2": [25, 14, 25]}
Output
{'marks1': [28, 69], 'marks2': [14]}

Approach

  1. For each list keep items whose count is 1.

Solution

Python
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]}
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs