0

I want to remove duplicate values next to each other, within a list inside a dictionary in python. Give me the best optimized mechanism to achieve this required output.

sample input

mydir = {
    "p1":[0,1,1,1,2,2,2,3,3,2,2,1,1],
    "p2":[1,2,2,1,1,2,2,3,3,3,0,0]
}

required output

mydir = {
    "p1":[0,1,2,3,2,1],
    "p2":[1,2,1,2,3,0]
}
  • 2
    The dictionary is irrelevant. Figure out how to do it for a single list, then iterate over the dictionary to do it to all the elements. – Barmar Jul 15 '21 at 05:54
  • You can check out `itertools.groupby`, e.g. `{k: [u for u, _ in groupby(v)] for k, v in mydir.items()}`. – hilberts_drinking_problem Jul 15 '21 at 05:55
  • You can try like `[c for i, c in enumerate(lst) if lst[i-1] !=c]` – pvc Jul 15 '21 at 06:05
  • mydir = { "p1":[0,1,1,1,2,2,2,3,3,2,2,1,1], "p2":[1,2,2,1,1,2,2,3,3,3,0,0] } for key,value in mydir.items(): print(key) print(value) value = list(set(value)) mydir[key]=value print(value) print(mydir) – Shivam Tyagi Jul 15 '21 at 09:13

0 Answers0