Remove all duplicates and permutations in a nested list in Python


Removing duplicates and changes from a settled list in Python could be a common errand that makes a difference streamline information and maintain a strategic distance from excess or dreary components. In this article, we point to extricate an interesting set of sublists from the settled list, disposing of any duplicates or stages. By doing so, we will streamline further operations and guarantee information keenness. In this article, we are going investigate three diverse approaches to attain this objective. Each approach will go with step-by-step clarifications, Python code, and yield, empowering you to get it and execute the foremost reasonable strategy for your particular needs.

Approach 1: Flattening and Converting to Set

This approach includes flattening the nested list into a single list and after that converting it to a set to remove duplicates. Here are the steps to execute this approach −

Algorithm

  • Step 1 − Make a function called flatten_list that takes a settled list as input.

  • Step 2 − Initialize an empty list, flattened_list, to store the flattened components.

  • Step 3 − Iterate through each component within the nested list.

    • a. In case the component may be a list, recursively call the flatten_list function on it.

    • b. On the off chance that the component isn't a list, add it to the flattened_list.

  • Step 4 − Return the flattened_list.

  • Step 5 − After getting the flattened list, change over it to a set utilizing the set() function.

  • Step 6 − Change over the set back to a list to protect the order of components. 

Example

def flatten_list(nested_list):
    flattened_list = []

    for element in nested_list:
        if isinstance(element, list):
            flattened_list.extend(flatten_list(element))
        else:
            flattened_list.append(element)

    return flattened_list

nested_list = [13, 2, [3, 45, [5, 56]], [7, 68, [9, 10]]]
flattened_list = flatten_list(nested_list)
result = list(set(flattened_list))

print(result)

Output

[2, 3, 68, 5, 7, 9, 10, 45, 13, 56]

Approach 2: By Using Recursion

In this approach, we characterize a recursive work remove_duplicates_permutations() that takes a settled list as input. Interior the work, we begin with straightening the settled list into a single list utilizing list comprehension, comparable to the past approach.

The stages variable holds the list of changes, but it may contain copies. To evacuate the copies, we change over the list of stages into a set and after that back to a list. This step guarantees that we as it were have interesting changes.

At long last, we repeat the one-of-a-kind changes and isolate each change into sublists of the same length as the initial sublists within the settled list. The coming about the list is our craved settled list without copies and stages. 

Algorithm

The moment approach includes utilizing recursion work to evacuate copies and stages from a settled list.

  • Step 1 − Define a function remove_duplicates_permutations_helper() that takes a settled list as inpu

  • Step 2 − Flatten the settled list into a single list.

  • Step 3 − Utilize the list comprehension to create all conceivable stages of the level list.

  • Step 4 − Remove copies from the list of changes.

  • Step 5 − Convert the remaining changes back into a settled list.

Example

def remove_duplicates_permutations_helper(sublist):
    sublist.sort()
    sublist = [sublist[i] for i in range(len(sublist)) if i == 0 or sublist[i] != sublist[i - 1]]
    return sublist

def remove_duplicates_permutations(nested_list):
    for i in range(len(nested_list)):
        nested_list[i] = remove_duplicates_permutations_helper(nested_list[i])
    return nested_list

# Example usage
nested_list = [[1, 25, 1], [42, 3, 44, 4], [5, 6, 76, 5]]
result = remove_duplicates_permutations(nested_list)
print(result)

Output

[[1, 25], [3, 4, 42, 44], [5, 6, 76]]

Approach 3: Using Nested Loops and List Comparison

In this approach, we repeat over each sublist within the nested list utilizing the primary circle. For each sublist, we compare it with the ensuing sublists utilizing the moment circle.

Algorithm

The third approach includes utilizing settled circles and list comparison to evacuate copies and stages from a settled list.

  • Step 1 − Define the function named remove_duplicates_permutations(). Iterate over each sublist in the nested list.

  • Step 2 − Compare each sublist with the subsequent sublists.

  • Step 3 − Remove any duplicate or permutation sublists found.

  • Step 4 − Return the modified nested list.

Example

def remove_duplicates_permutations(nested_list):
    result = []
    for i in range(len(nested_list)):
        is_duplicate = False
        for j in range(i + 1, len(nested_list)):
            if nested_list[i] == nested_list[j]:
                is_duplicate = True
                break
        if not is_duplicate:
            result.append(nested_list[i])
    return result

# Example usage
nested_list = [[1, 25, 13], [32, 3, 64], [3, 64, 15]]
result = remove_duplicates_permutations(nested_list)
print(result)

Output

[[1, 25, 13], [32, 3, 64], [3, 64, 15]]

Conclusion

In this article, we investigated three diverse approaches to evacuate copies and stages from a settled list in Python. Each approach displayed a calculation and gave step-by-step clarifications. We secured the language structure and code usage for each approach, at the side of the comparing yield.

Updated on: 04-Sep-2023

500 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements