Home > Enterprise >  Merge 2D list if duplicate for multiple value
Merge 2D list if duplicate for multiple value

Time:07-07

[[20, 2, 2, 5], [20, 2, 2, 5], [40, 2, 2, 2, 5]]

So, I know dictionary can be used in some of cases. But how will I do it? I want to add the first element to the inside list if two element is complete similar So the solution will be:

[[40,2,2,5],[40,2,2,2,5]]

CodePudding user response:

One way is to add each row to dictionary (in the key part). The dictionary will remove all the duplicates. Another way is to add all the rows to a set. And then convert it back to a list . Duplicates will be gone as set removes duplicates.

CodePudding user response:

Creating a empty list, then using if element not in list: list.append(element).

  • Related